Skip to content

Form and async hooks

The React hooks behind forms that save and previews, such as useSaveQueue and useDraftDirty.

API

Functions

FunctionSignatureDoes
useSaveQueue(submit: () => void, saving: boolean): () => voidSave-on-change without racing saves: call the returned change() from each control's handler. submit posts the form as it stands; saving is the fetcher's or navigation's busy state, and its fall to false settles the save in flight and sends any change made meanwhile.
useDraftDirty(saved: unknown): { dirty: boolean; edit: () => void; submit: () => void; }Whether a form holds edits its last successful save did not cover. Call edit() from the form's change handler and submit() from its submit; pass the save's answer as saved when it succeeded (a new object each save), and an edit typed while that save was in flight stays dirty.
useDebounced<T>(value: T, ms: number): Tvalue, once it has held still for ms. The first render answers it at once.
useDebouncedPreview<R>(request: string | null, read: (request: string, signal: AbortSignal) => Promise<R>, delay?: number): DebouncedPreview<R>A preview a beat after the input stops changing: the server's rendering of a form, a search as you type. request is the serialized input (null asks for nothing); read fetches it, and a request overtaken by a newer one is aborted, so a slow answer never lands over a newer one. busy is derived: the current request is not yet the one an answer settled.
createSaveQueue(submit: () => void): SaveQueueSaves on every change without two saves racing. A form that saves as each control changes keeps its controls enabled during a save (disabling the control that has focus throws focus to the page body), so a change can arrive mid-save. Sending a second request at once would let two saves land in either order; this holds every change made mid-save and sends one more save after the first settles. submit posts what the controls show at that moment, so the last value wins.
draftAfter(state: DraftState, event: DraftEvent): DraftStateThe reducer: an edit typed while a save is in flight stays unsaved after that save lands.
draftDirty(state: DraftState): boolean
filterParams(from: URLSearchParams, values: Readonly<Record<string, string>>, keep?: readonly string[], fixed?: Readonly<Record<string, string>>): URLSearchParamsSearch parameters with values set (trimmed, empty ones dropped) over the keep names of from.
withParam(pathname: string, search: string, name: string, value: string | null): stringThe address pathname?search with name set to value, or removed when it is null.
pagePending(navigation: { state: string; location?: { pathname: string; } | undefined; }, revalidatorState: string, pathname: string): booleanWhether a page's own data is being read again (the revalidator, or a navigation that lands on this same page), not whether the reader is leaving for another page. "/a/" is the same page as "/a".