Defjs
React

useClient

React Hook that returns an explicit Defjs client or the nearest ClientProvider.

@defjs/react · Function

Signature

function useClient(overrideClient?: DerivableClient): DerivableClient;

useClient(overrideClient) prefers the exact explicit client; omitted or undefined reads the nearest ClientProvider. An Error is thrown only when neither source supplies a client. Zero-argument useClient() calls remain valid.

Always call useClient(explicitClient) at the top level of a component or custom Hook, even with an explicit client; not in an ordinary function or event callback. Do not write explicitClient ?? useClient(), which conditionally calls a Hook. The override does not write Context or create a provider, and the Hook still subscribes to Context.

The returned value is the exact DerivableClient that was supplied. Call it with request options at the call site to derive a Client value for one call: getUser(client({ signal }), input). It is a function, so do not pass it to useState or a state setter: React calls a function argument as a lazy initializer or an updater. The provider holds only a root client; a derived Client value is not accepted, so create it where the operation is called.

Parameters

Prop

Type

overrideClient? DerivableClient

Derivable client to use before context; undefined uses the provider.

Returns

The selected derivable client.

Throws

If neither an explicit client nor a ClientProvider is available.

Examples

const client = useClient()
Edit this page on GitHub
Released under the MIT License. · Copyright © 2026 MunMunMiao