React Query
el-form-react-query connects a record query to an El Form submission without making either package own your server API. QueryForm loads one record, turns it into editable input values, then gives save the schema's parsed output.
This package is currently an unreleased workspace package on the December 1, 2026 local roadmap branch. In this repository, add it to an application workspace alongside the dependencies it needs.
pnpm --filter your-app add "el-form-react-query@workspace:*" @tanstack/react-query@^5
The adapter is optional. It declares TanStack Query and React as peer dependencies. This example uses a Zod schema for parsed submissions.
Use the full record as the query result and put any projection into toValues. The adapter uses the key hashing policy set on your QueryClient. The per-query select and queryKeyHashFn options are excluded. Each key identifies one independently editable record.
Edit a server record
This example starts with a numeric seats value from the server. The input holds text, and the Zod schema converts it to a number before save runs. The save callback returns the complete canonical record, which updates the matching query cache entry.
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { useState } from "react";
import { QueryForm } from "el-form-react-query";
import { z } from "zod";
const reservationSchema = z.object({
guest: z.string().trim().min(2),
seats: z
.string()
.regex(/^[1-9]\d*$/)
.transform(Number)
.refine(Number.isSafeInteger),
});
type Reservation = { id: string; guest: string; seats: number };
function ReservationEditor({ id }: { id: string }) {
return (
<QueryForm
schema={reservationSchema}
query={{
queryKey: ["reservation", id],
queryFn: () => api.reservations.get(id),
}}
toValues={(reservation: Reservation) => ({
guest: reservation.guest,
seats: String(reservation.seats),
})}
save={(parsed) => api.reservations.update(id, parsed)}
pending={<p>Loading reservation…</p>}
renderError={(error, retry) => (
<button type="button" onClick={retry}>Retry. {error.message}</button>
)}
>
{({ form, mutation, handleSubmit }) => (
<form onSubmit={handleSubmit}>
<label>
Guest <input {...form.register("guest")} />
</label>
<label>
Seats <input inputMode="numeric" {...form.register("seats")} />
</label>
<button disabled={mutation.isPending} type="submit">
{mutation.isPending ? "Saving…" : "Save"}
</button>
</form>
)}
</QueryForm>
);
}
export function App() {
const [queryClient] = useState(() => new QueryClient());
return (
<QueryClientProvider client={queryClient}>
<ReservationEditor id="reservation-42" />
</QueryClientProvider>
);
}
QueryForm calls form.handleSubmit itself. An invalid local value never reaches the mutation. A valid value reaches save as { guest: string, seats: number }, so the server boundary receives the parsed schema output instead of browser strings.
Loading the local reservation example…
The example is local and in-memory. Its first save succeeds and the Parsed save payload receipt shows that seats arrived as a number. Use Make next save fail to see a mutation error remain visible, then save again to retry. Refresh record updates the server's untouched note and invalidates the exact reservation key. Edit Guest, then refresh, to see the dirty guest input stay in place while the clean note receives the response. The canonical cache receipt below the form shows the record returned by the server.
Data ownership and refreshes
query.queryKey identifies both the loaded record and the cache write after a save. Keep every editable record's key specific, such as ["reservation", id]. When the key changes, QueryForm creates a form for the newly loaded record. A late save for the previous key updates only its original cache entry.
By default, keepDirtyValues is true. Background refetches update untouched values and preserve the values someone is currently editing. Pass keepDirtyValues={false} when the remote record must replace local edits.
<QueryForm
schema={reservationSchema}
query={{ queryKey: ["reservation", id], queryFn: () => api.reservations.get(id) }}
toValues={toReservationInput}
save={(parsed) => api.reservations.update(id, parsed)}
keepDirtyValues={false}
>
{renderReservation}
</QueryForm>
After a successful save, the returned record becomes the new form baseline. If someone changes a field while a save is pending, that newer edit stays dirty rather than being overwritten by the earlier response.
For a save button outside the form, associate it with the native form using form="reservation-form" and set that ID on the form. Calling the element's requestSubmit() also runs the same parsed-save handler. Each query key keeps its own cache record. If two editors save the same record through the same QueryClient, an older response cannot replace a later confirmed save.
Loading and error UI
pending and renderError make the first query state explicit. renderError receives the query error and a retry callback. Mutation errors remain on mutation.error, which lets the form display them next to the submit control and keep all current field values available for a retry.
<QueryForm
// schema, query, toValues, and save
pending={<ReservationSkeleton />}
renderError={(error, retry) => (
<section role="alert">
<p>Could not load this reservation. {error.message}</p>
<button type="button" onClick={retry}>Try again</button>
</section>
)}
>
{({ form, mutation, handleSubmit }) => (
<form onSubmit={handleSubmit}>
{/* registered fields */}
{mutation.error instanceof Error && <p role="alert">{mutation.error.message}</p>}
<button disabled={mutation.isPending}>Save</button>
</form>
)}
</QueryForm>
Use ordinary TanStack Query features around the adapter. Set stale time, invalidate related lists, add optimistic cache updates, and configure retries in the QueryClient or the standard query options. QueryForm supplies the record-to-form boundary and safe parsed submission.