Skip to content

React provider and hooks

Connect React applications to the SDK: the provider, read and workflow hooks, and incremental validation.

One AnvilProvider near the root of the tree makes a configured SDK available to everything beneath it, and useAnvilSDK returns it — throwing if a component renders outside the provider; useAnvilSDKOptional returns undefined instead when that is more convenient.

Read hooks such as useCollateralTokens, useTokenPrice, and useRequiredCollateral wrap the core actions and carry their own loading and error state. Workflow hooks such as useCreateLOCWorkflow validate a form, report per-field issues, and hand back a workflow only once the params are valid — useIncrementalValidation drives that per-field state from the core validation rule sets directly, for a form that does not need a full workflow hook.

The beta components built on these hooks — CreateLOCFlow and the rest — are in React components; the instance these hooks need is built in Setup and configuration.

Provider at the root, hook in a component:

export function App() {
// Build the SDK once (module scope or useMemo); hooks key on it.
return (
<AnvilProvider sdk={sdk}>
<LOCBeneficiary id={1n} />
</AnvilProvider>
);
}
function LOCBeneficiary({ id }: { id: bigint }) {
// Throws if this component renders outside <AnvilProvider>.
const sdk = useAnvilSDK();
const [loc, setLoc] = useState<OutstandingLetterOfCredit>();
useEffect(() => {
void sdk.loc.getOutstandingLetterOfCredit({ id }).then(setLoc);
}, [sdk, id]);
return <p>Beneficiary: {loc?.beneficiary ?? 'loading'}</p>;
}

Primary APIs

Provider

  • AnvilProvider — React context provider that makes one configured AnvilSDK available to every hook and component beneath it.
  • useAnvilSDK — Returns the AnvilSDK instance from context — the one passed to <AnvilProvider sdk={...}> — so sdk.loc, sdk.vault, sdk.pricing,…
  • useAnvilSDKOptional — Returns the AnvilSDK instance from context, or null if no provider is present.

Read hooks

Workflow hooks

Validation

Workflow settlement

Utilities

  • useResolveAddress — Hook that returns the resolveAddress action configured with the current Anvil SDK context.
  • useEnsName — Hook that returns the getEnsName action configured with the current Anvil SDK context.
  • useNowSeconds — The current Unix time in whole seconds, re-rendering only at the moments a caller names.
  • bindOnAccountChanged — Binds an event listener to the accountsChanged event of the injected browser wallet (window.ethereum), if it exists.
  • unbindOnAccountChanged — Unbinds an event listener from the accountsChanged event of the injected browser wallet (window.ethereum), if it exists.

Supporting types and functions in this group are not listed here; each has its own page, listed with the group in the sidebar.