Medplum React Hooks Library
npm install @medplum/react-hooksThe Medplum React Hooks Library provides non-UI React features for your application.
Most users will want the full Medplum React Component Library, @medplum/react. However, that library has peer dependencies on Mantine, which may not be desired.
- useMedplum - handles shared global instance of MedplumClient
- 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
Add as a dependency:
```
npm install @medplum/react-hooks
Note the following peer dependencies:
- @medplum/core
- react
- react-dom
`tsx
import { MedplumClient } from '@medplum/core';
import { MedplumProvider } from '@medplum/react';
const medplum = new MedplumClient();
export function App() {
return (
);
}
`
For more details on how to setup MedplumClient, refer to the docs for medplum.
`tsx
import { useMedplum } from '@medplum/react-hooks';
export function MyComponent() {
const medplum = useMedplum();
return
useMedplumContextuseMedplumContext can be used to access the MedplumContext provided by the MedplumProvider directly. The MedplumContext has the following interface:`ts
interface MedplumContext {
medplum: MedplumClient;
navigate: MedplumNavigateFunction;
profile?: ProfileResource;
loading: boolean;
}
`$3
You can use the
loading property from useMedplumContext() to know when MedplumClient 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.`tsx
function MyComponent(): JSX.Element {
const { loading } = useMedplumContext();
return loading ? : Loaded!;
}
`useSubscriptionuseSubscription creates an in-memory Subscription resource with the given criteria on the Medplum 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.
`tsx
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
Notifications received: {notificationCount};
}
`useSubscription docs$3
Usage within
Expo / React Native` has some special considerations. See: @medplum/expo-polyfills READMEMedplum is a healthcare platform that helps you quickly develop high-quality compliant applications. Medplum includes a FHIR server, React component library, and developer app.
Apache 2.0. Copyright © Medplum 2025