Skip to content

API ReferenceReact provider and hooksFunction

useAnvilSDKOptional()

function useAnvilSDKOptional(): AnvilSDK | null;

Returns the AnvilSDK instance from context, or null if no provider is present. Useful for components that work both inside and outside an <AnvilProvider> (e.g. presentational components with optional SDK-backed features).

Unlike useAnvilSDK this never throws, so a component using it must handle the null branch itself rather than assuming a provider.

Returns

AnvilSDK | null

The provider’s SDK instance, or null when there is no provider above this component or the nearest one carries sdk={null}.

Example

export function ChainBadge() {
// `null` outside <AnvilProvider>, so this renders in isolation too.
const sdk = useAnvilSDKOptional();
if (!sdk) return <span>Not connected</span>;
return <span>Chain {sdk.deps.profile.chainId}</span>;
}