useClient

function useClient<TClient>(): Client<TClient>;

Reads the Kit client published by the nearest ClientProvider. Throws a SolanaError with code SOLANA_ERROR__REACT__MISSING_PROVIDER if no provider is mounted in the ancestor tree.

Pass the shape of your client through the generic (typically the AppClient type you export alongside the client) so every installed capability is typed at the call site. This is a pure cast with no runtime capability check, so reach for useClientCapability when a missing plugin should fail loudly at mount instead of surfacing later as undefined.

Type Parameters

Type ParameterDescription
TClient extends objectThe shape the client is expected to satisfy. Pure type assertion, so this must always be supplied — the hook can't infer it from its (absent) arguments.

Returns

Client<TClient>

Example

import { ClientWithRpc, GetEpochInfoApi } from '@solana/kit';
import { useClient } from '@solana/react';
 
function ManualSend() {
    const client = useClient<ClientWithRpc<GetEpochInfoApi>>();
    return <button onClick={() => client.rpc.getEpochInfo().send()}>Fetch</button>;
}

See

On this page