React integration
Provide the Ovok client to React components and use the SDK hooks.
- Stable unknown
- Release candidate since 0.4.49-rc.2
- Development since 0.4.49-dev.493+062a3db
Complexity: 2 of 5 · Client: React (@ovok/core/react) · Time: about 10 minutes
Provide the clientClient-side
Create the client from @ovok/core and pass it to OvokProvider from @ovok/core/react. Keep the client stable across renders.
'use client';
import { useState, type PropsWithChildren } from 'react';
import { Ovok } from '@ovok/core';
import { OvokProvider } from '@ovok/core/react';
export function AppProviders({ children }: PropsWithChildren) {
const [client] = useState(() => new Ovok({
baseUrl: 'https://api.sandbox.ovok.com',
tenantCode: '{{TENANT_CODE}}',
}));
return <OvokProvider client={client}>{children}</OvokProvider>;
}The React entry uses the host application's React installation.
Read data with the hooksClient-side
Components below the provider can use the session and FHIR hooks.
'use client';
import { useSearch } from '@ovok/core/react';
export function PatientList() {
const { data, loading, error, refetch } = useSearch('Patient', { _count: 10 });
if (loading) return <p>Loading patients…</p>;
if (error) return <button onClick={refetch}>Try again</button>;
return <p>{data?.data.length ?? 0} patients</p>;
}In server-rendered applications, create user-specific clients in the correct request scope and keep machine secrets on the server. The provider and hooks manage access to the client; they do not grant server authorization.
Read the signed-in profileClient-side
useProfile() returns the signed-in profile reference, or null when signed out, in the same loading and error state as the other query hooks.
'use client';
import { useProfile } from '@ovok/core/react';
export function ProfileReference() {
const { data: profile, loading } = useProfile();
if (loading) return null;
return <p>{profile ? profile.display ?? profile.reference : 'Signed out'}</p>;
}Show unsent measurementsClient-side
useOutbox() returns what the outbox holds and updates as it changes. It is undefined until the first snapshot.
'use client';
import { useOutbox } from '@ovok/core/react';
export function UnsentBadge() {
const outbox = useOutbox();
if (!outbox || outbox.pending + outbox.rejected === 0) return null;
return <p>{outbox.offline ? 'Offline: ' : ''}{outbox.pending} waiting, {outbox.rejected} need attention</p>;
}Test your integration
- Render
PatientListinsideAppProviderswhile signed in as a sandbox user. - Confirm that the loading text appears first and the patient count follows.
| Scenario | How to trigger it | Expected result |
|---|---|---|
| The search succeeds | Sign in a sandbox user who can read patients | The patient count renders |
| The search fails | Point baseUrl at a host that does not answer | The "Try again" button renders |
See also
Last updated on