4.3.178 • Published 10 months ago

@iehr/react-hooks v4.3.178

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
10 months ago

iEHR React Hooks Library

The iEHR React Hooks Library provides non-UI React features for your application.

Most users will want the full iEHR React Component Library, @iehr/react. However, that library has peer dependencies on Mantine, which may not be desired.

Key Features

  • useIEHR - handles shared global instance of IEHRClient
  • useResource - reads a resource by ID or reference with intelligent caching
  • useSearch - performs a FHIR search with intelligent state management
  • useSubscription - subscribes to a FHIR search criteria and calls a given callback upon receiving a relevant notification

Installation

Add as a dependency:

npm install @iehr/react-hooks

Note the following peer dependencies:

Setup

import { IEHRClient } from '@iehr/core';
import { IEHRProvider } from '@iehr/react';

const iehr = new IEHRClient();

export function App() {
  return (
    <IEHRProvider iehr={iehr}>
      <MyPage1 />
      <MyPage2 />
      <Etc />
    </IEHRProvider>
  );
}

For more details on how to setup IEHRClient, refer to the docs for iehr.

useIEHR

import { useIEHR } from '@iehr/react-hooks';

export function MyComponent() {
  const iehr = useIEHR();
  return <div>{JSON.stringify(iehr.getProfile())}</div>;
}

useIEHRContext

useIEHRContext can be used to access the IEHRContext provided by the IEHRProvider directly. The IEHRContext has the following interface:

interface IEHRContext {
  iehr: IEHRClient;
  navigate: IEHRNavigateFunction;
  profile?: ProfileResource;
  loading: boolean;
}

Using loading to know when IEHRClient initialization is done

You can use the loading property from useIEHRContext() to know when IEHRClient has finished initialization successfully. loading is updated asynchronously so it will usually start as false and change to true once the client has finished its initialization.

function MyComponent(): JSX.Element {
  const { loading } = useIEHRContext();
  return loading ? (
    <Spinner />
  ) : (
    <div>Loaded!</div>
  );
}

useSubscription

useSubscription creates an in-memory Subscription resource with the given criteria on the iEHR server and calls the given callback when an event notification is triggered by a resource interaction over a WebSocket connection.

Subscriptions created with this hook are lightweight, share a single WebSocket connection, and are automatically untracked and cleaned up when the containing component is no longer mounted.

function MyComponent(): JSX.Element {
  const [notificationCount, setNotificationCount] = useState(0);

  useSubscription(
    'Communication?sender=Practitioner/abc-123&recipient=Practitioner/me-456', 
    (bundle: Bundle) => {
      console.log('Received a message from Practitioner/abc-123!');
      handleNotificationBundle(bundle); // Do something with the bundle
      setNotificationCount(s => s + 1);
    }
  );

  return <div>Notifications received: {notificationCount}</div>;
}

Subscription Extensions

Any Subscription extension supported by iEHR can be attached to a Subscription created by the useSubscription hook via a 3rd optional parameter to the hook, options, which takes an optional subscriptionProps.

type UseSubscriptionOptions = {
  subscriptionProps?: Partial<Subscription>;
}

Here's how you would subscribe to only create interactions for a criteria:

const createOnlyOptions = {
  subscriptionProps: {
    extension: [
      {
        url: 'https://iehr.ai/fhir/StructureDefinition/subscription-supported-interaction',
        valueCode: 'create',
      },
    ],
  }
};

function MyComponent(): JSX.Element {
  const [createCount, setCreateCount] = useState(0);

  useSubscription(
    'Communication?sender=Practitioner/abc-123&recipient=Practitioner/me-456',
    (_bundle) => {
      console.log('Received a new message from Practitioner/abc-123!');
      setCreateCount(s => s + 1);
    },
    createOnlyOptions,
  );

  return <div>Create notifications received: {createCount}</div>;
}

Subscriptions with the same criteria are tracked separately if they have differing subscriptionProps. This means you can create one Subscription to listen for create interactions and another for update interactions and they will not interfere with each other.

const createOnlyOptions = {
  subscriptionProps: {
    extension: [
      {
        url: 'https://iehr.ai/fhir/StructureDefinition/subscription-supported-interaction',
        valueCode: 'create',
      },
    ],
  }
};

const updateOnlyOptions = {
  subscriptionProps: {
    extension: [
      {
        url: 'https://iehr.ai/fhir/StructureDefinition/subscription-supported-interaction',
        valueCode: 'update',
      },
    ],
  }
};

function MyComponent(): JSX.Element {
  const [createCount, setCreateCount] = useState(0);
  const [updateCount, setUpdateCount] = useState(0);

  useSubscription(
    'Communication?sender=Practitioner/abc-123&recipient=Practitioner/me-456',
    (_bundle) => {
      console.log('Received a new message from Practitioner/abc-123!');
      setCreateCount(s => s + 1);
    },
    createOnlyOptions,
  );

  useSubscription(
    'Communication?sender=Practitioner/abc-123&recipient=Practitioner/me-456',
    (_bundle) => {
      console.log('Received an update to message from Practitioner/abc-123!');
      setUpdateCount(s => s + 1);
    },
    updateOnlyOptions,
  );

  return (
    <>
      <div>Create notifications received: {createCount}</div>
      <div>Update notifications received: {updateCount}</div>
    </>
  );
}

Usage within Expo app

Usage within Expo / React Native has some special considerations. See: @iehr/expo-polyfills README

About iEHR

iEHR is a healthcare platform that helps you quickly develop high-quality compliant applications. iEHR includes a FHIR server, React component library, and developer app.

License

Apache 2.0.

4.3.176

10 months ago

4.3.177

10 months ago

4.3.178

10 months ago

4.3.175

10 months ago

4.3.174

10 months ago

4.3.151

11 months ago

4.3.170

10 months ago

4.3.150

11 months ago

4.3.160

11 months ago

4.3.141

11 months ago

4.3.110

11 months ago

4.3.91

11 months ago

4.3.90

11 months ago

4.3.140

11 months ago

4.3.120

11 months ago

4.3.82

12 months ago

4.3.81

12 months ago

4.3.80

12 months ago

4.3.84

11 months ago

4.3.83

11 months ago

3.2.2

1 year ago

3.2.1

1 year ago

3.1.11

1 year ago

3.2.0

1 year ago

4.3.60

1 year ago

4.3.70

12 months ago

3.1.10

1 year ago

4.3.63

12 months ago

4.3.52

1 year ago

4.3.62

1 year ago

4.3.51

1 year ago

4.3.40

1 year ago

4.3.61

1 year ago

4.3.50

1 year ago

3.1.3

1 year ago

3.1.1

1 year ago

3.0.8

1 year ago

3.0.7

1 year ago

3.0.4

1 year ago