useOvokRequest
Wraps an asynchronous loader with data, loading, error, and manual reload state.
When to use it
Keep load stable with useCallback. enabled false skips loading; this primitive adds no retry policy or response cache.
Setup
This generic hook accepts a loader and does not read OvokProvider context. Use it from a client-side custom hook and keep the loader stable with useCallback.
Hook arguments
| Argument | Type | Details |
|---|---|---|
load | () => Promise<T> | Async loader called by the hook; memoize it with useCallback. |
enabled (optional) | boolean | Set false to skip loading; the hook reports loading false and clears the error. |
TypeScript signature
<T>(load: () => Promise<T>, enabled?: boolean): OvokRequestState<T>
Request state
The response payload is T. A successful empty result, a failed request, and a request that has not returned data are distinct states.
| Field | Meaning |
|---|---|
data | Latest successful response, or undefined before one is available. |
loading | Whether the request is in progress. |
error | Request error as an Error, or undefined when no request has failed. |
reload | Repeats the request with the current arguments. |
Request behavior
The hook loads when its required inputs are available and reloads when those inputs change. Use reload for an explicit refresh; render loading, error, and successful no-result states separately.
Example
This custom hook keeps the loader stable with useCallback and returns request state for its caller to render.
import { useCallback } from 'react';
import { useClient, useOvokRequest } from '@ovok/core';
export function useProjectSettingsRequest() {
const client = useClient();
const load = useCallback(() => client.getProjectSettings(), [client]);
return useOvokRequest(load);
}
Related
- Generated API reference for overloads and exact input and response types.
- React applications for provider setup and application patterns.
- Hook guide index for related hooks.