React Router
Prefetch ENS reads in a React Router or Remix loader and hydrate them in the route component, so names render without a loading state.
In React Router (framework mode) and Remix, prefetch the reads in a route loader, return the state, and render HydrationBoundary in the route component.
Server rendering helpers are unstable
prefetch, dehydrate and HydrationBoundary are marked unstable. Their names and shapes can
change in a minor release.
1. Provide the client in the root
Create the client in a shared module and render the provider in root.tsx, around the <Outlet />.
Example (The Root Route)
import { Outlet } from "react-router";
import { EnsforgeProvider } from "@ensforge/react";
import { ens } from "./lib/ens";
export default function Root() {
return (
<EnsforgeProvider client={ens}>
<Outlet />
</EnsforgeProvider>
);
}import { createPublicClient, http } from "viem";
import { mainnet } from "viem/chains";
import { Ensforge } from "@ensforge/sdk";
export const ens = Ensforge.create({
publicClient: createPublicClient({ chain: mainnet, transport: http() }),
});2. Prefetch in the loader
Create a new registry in each loader call. The loader returns plain JSON, so the state serializes to the browser.
Example (A Name Route)
import { AtomRegistry } from "effect/reactivity";
import { useLoaderData, type LoaderFunctionArgs } from "react-router";
import { HydrationBoundary, useName } from "@ensforge/react";
import { dehydrate, prefetch } from "@ensforge/react/server";
import { Actions } from "@ensforge/sdk";
import { ens } from "../lib/ens";
export async function loader({ params }: LoaderFunctionArgs) {
const name = String(params.name);
const registry = AtomRegistry.make();
await prefetch(registry, ens, Actions.name.get, { name });
return { name, state: dehydrate(registry) };
}
function Profile({ name }: { name: string }) {
const { data } = useName({ name });
return <p>{data?.owner}</p>;
}
export default function Route() {
const { name, state } = useLoaderData<typeof loader>();
return (
<HydrationBoundary state={state}>
<Profile name={name} />
</HydrationBoundary>
);
}useNamereads the same action and options asprefetch, so it uses the prefetched value.- A failed read is not sent, so the route retries it in the browser.
For streaming, call dehydrate(registry, { streaming: true }) and do not await prefetch, as in Next.js App Router. Reads still loading then stream to the browser as they resolve.
Streaming puts real Promises in the state, so a loader must return it through a serializer that streams promises. React Router's turbo-stream encoding should do this by its documented behaviour, but this page has not verified it. A plain JSON response drops the promise without an error, and the browser then fetches the read itself.
Was this page helpful?