Skip to content

React wrapper for the IoT Open Lynx APIs

License

Notifications You must be signed in to change notification settings

IoTOpen/react-lynx

Repository files navigation

React Lynx

This is a React hooks wrapper for the node-lynx library. This library exposes most API-calls as hooks that return objects with named properties. It also includes Contexts that can be used to handle Lynx-connection and current user.

All hooks need to be used withing the LynxProvider context.

Install

npm install --save @iotopen/react-lynx

Usage

Create a LynxProvider to wrap all components using the hooks:

const key = 'abcdef123456789abcdef123456789';

const App = () => {
    return (
        <LynxProvider apiKey={key} apiURL={'https://lynx.iotopen.se'}>
            <MyComponent/>
        </LynxProvider>
    );
};

Then the hooks can be used in MyComponent:

import {useInstallations} from '@iotopen/react-lynx';

const MyComponent = () => {
    const {loading, error, installations, refresh} = useInstallations();

    return (
        <div>
            {loading && <h2>Loading installations...</h2>}
            {!loading && <ul>
                {installations.map(i => <li>{i.name}</li>)}
            </ul>}
        </div>
    );
};