--- title: "React overview" description: "Choose Beignet React integrations for server state, URL state, and forms while reusing contract schemas and types." url: "https://www.beignetjs.com/react" markdown_url: "https://www.beignetjs.com/markdown/react.md" framework_version: "0.0.65" documentation_scope: "current-checkout" source_revision: "e5b4771b262efbdd036e3a40a514c08d1508316c" source_modified: true --- # React Beignet has optional React integrations for server state, URL state, forms, and uploads. Each package is independent, so install only the pieces your app needs. Two form adapters exist; the starter generates React Hook Form, and TanStack Form is an alternative that binds the same contracts. Choose one for new forms; existing forms can keep their adapter during a migration. | Package | Use it for | | ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ | | [`@beignet/react-query`](https://www.beignetjs.com/markdown/react-query.md) | Typed TanStack Query options, mutations, prefetching, and query keys | | [`@beignet/nuqs`](https://www.beignetjs.com/markdown/nuqs.md) | URL-backed search, filters, tabs, sorting, and pagination | | [`@beignet/react-hook-form`](https://www.beignetjs.com/markdown/react-hook-form.md) | Typed React Hook Form setup from a contract body schema | | [`@beignet/react-form`](https://www.beignetjs.com/markdown/react-form.md) | Typed TanStack Form setup from a contract body schema | | [`@beignet/react-uploads`](https://www.beignetjs.com/markdown/react-uploads.md) | Typed upload state, progress, errors, and results from a Beignet upload client | ## Adapter shape Every adapter follows the same shape: create the package adapter once, then bind contracts or upload names from the returned helper. ```typescript const rq = createReactQuery(client); const rhf = createReactHookForm(); // or createReactForm() for TanStack Form const nq = createNuqs(); const reactUploads = createReactUploads({ uploads }); ``` ## Feature workflow Keep shared adapter factories in `client/` and product UI in the feature: ```txt client/ auth-client.ts forms.ts index.ts features/ todos/ components/ todo-app.tsx contracts.ts ``` Use contracts directly with the shared adapters. Extract [feature client helpers](https://www.beignetjs.com/markdown/react-query.md#feature-client-helpers) when sharing defaults or cache behavior. ```tsx "use client"; import { useMutation, useQuery } from "@tanstack/react-query"; import { rq } from "@/client"; import { rhf } from "@/client/forms"; import { createTodo, listTodos } from "@/features/todos/contracts"; const createTodoForm = rhf(createTodo); export function TodoApp() { const todosQuery = useQuery(rq(listTodos).queryOptions({ query: {} })); const form = createTodoForm.useForm({ defaultValues: { title: "" } }); const createTodoMutation = useMutation( rq(createTodo).mutationOptions({ invalidates: () => [rq(listTodos).contractFilter()], onSuccess: () => form.reset(), }), ); const onSubmit = form.handleSubmit((body) => { createTodoMutation.mutate({ body }); }); return
; } ``` This is feature colocation, not server colocation. Components can import contracts, feature client helpers, and frontend helpers, but they should not import use cases, route groups, infra adapters, provider packages, `server/`, or `app-context.ts`. ## Server context and prefetching When a Next.js layout or Server Component needs request-scoped Beignet state, use an app-owned server-only helper such as `lib/server-context.ts` that wraps `server.createContextFromNext()` in React `cache(...)`. Reading `ctx.auth`, `ctx.tenant`, or request metadata there is fine for redirects and shell state; feature data and business workflows should still go through use cases. When a Server Component needs to hydrate React Query data and the route is a thin `{ contract, useCase }` binding, keep the contract-derived query key from `rq(contract).queryOptions(...)` and replace only the server `queryFn` with an app-owned `lib/server-react-query.ts` helper that consumes the request context. Use the normal HTTP `rq(contract).queryOptions(...)` path when the route handler owns response mapping, headers, streaming, or other HTTP-layer behavior. ## Install Install the integrations your app needs: ```bash bun add @beignet/react-query @tanstack/react-query bun add @beignet/nuqs nuqs bun add @beignet/react-uploads ``` For forms, choose one of the following. The starter already installs [React Hook Form](https://www.beignetjs.com/markdown/react-hook-form.md): ```bash bun add @beignet/react-hook-form react-hook-form@^7.55.0 @hookform/resolvers@^7.0.1 ``` For [TanStack Form](https://www.beignetjs.com/markdown/react-form.md) instead: ```bash bun add @beignet/react-form @tanstack/react-form@^1.33.5 ``` The [TanStack Form setup guide](https://www.beignetjs.com/markdown/react-form.md#setup) explains how to preserve existing starter forms during migration. Apps that depend on `@beignet/react-form` must add form components by hand; the CLI's `--with ui` addon and `--recipe full-slice` generate React Hook Form and stop in these apps.