Skip to main content

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​

ArgumentTypeDetails
load() => Promise<T>Async loader called by the hook; memoize it with useCallback.
enabled (optional)booleanSet 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.

FieldMeaning
dataLatest successful response, or undefined before one is available.
loadingWhether the request is in progress.
errorRequest error as an Error, or undefined when no request has failed.
reloadRepeats 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);
}