useRequest

function useRequest<T>(source, options?): RequestResult<T>;

Fire a one-shot request on mount and re-fire each time source changes identity or refresh() is called. Returns reactive state tracking the call's lifecycle.

The hook accepts any ReactiveActionSource — the { reactiveStore() } duck-type satisfied by PendingRpcRequest and other plugin-authored pending objects (e.g. a DAS client's getAsset(address)). Pass null to disable; the result reports status: 'disabled'.

Memoize the source with useMemo keyed on whatever inputs it depends on. Stable identity is how the hook knows when to re-fire — and because the deps live on a native useMemo, react-hooks/exhaustive-deps catches stale closures by default.

Type Parameters

Type ParameterDescription
TThe value the underlying request resolves to.

Parameters

ParameterType
sourceReactiveActionSource<T> | null
options?UseRequestOptions

Returns

RequestResult<T>

Example

function LatestBlockhash() {
    const client = useClientCapability<ClientWithRpc<GetLatestBlockhashApi>>({
        capability: 'rpc',
        hookName: 'useLatestBlockhash',
        providerHint: 'Install `solanaRpc()` on the client.',
    });
    const source = useMemo(() => client.rpc.getLatestBlockhash(), [client]);
    const { data, error, refresh } = useRequest(source, {
        perRequestSignal: () => AbortSignal.timeout(5_000),
    });
    if (error) return <button onClick={refresh}>Retry</button>;
    return <p>{data ? `Blockhash: ${data.value.blockhash}` : 'Loading…'}</p>;
}

See

On this page