useClient
Use useClient() in a React component when it needs the OvokClient configured for the surrounding application.
Before you use it
Render the component under OvokProvider. The provider supplies the client instance; useClient() reads that instance from React context. Keep the provider in a client-side part of the React tree.
Arguments
useClient() takes no arguments.
Return value
It returns the configured OvokClient instance. The hook does not create a new client, make a request, or start an authentication flow.
When to use it
Use this hook when a component needs to call a client operation directly, or when another hook needs the same configured client. Use the measurement-specific hooks when they already match the task your component performs.
For non-component code, pass or otherwise access the application’s OvokClient directly instead of calling a React hook.
Example
Use the client when a component needs an operation that does not have a dedicated hook:
import { useClient } from '@ovok/core';
export function SignOutButton() {
const client = useClient();
async function handleSignOut() {
try {
await client.logout();
} catch {
// The local session is cleared; show a retry message if your app needs one.
}
}
return <button onClick={() => void handleSignOut()}>Sign out</button>;
}
If the client is unavailable
Check that the component is below the intended OvokProvider, that the provider received the client for the active environment, and that the component is running on the client side. A provider boundary with a different client supplies that boundary’s instance to its descendants.
Related pages
- Generated API reference for the public return type.
- React applications
- useObservations
- useSaveMeasurement