Skip to content

Form Reset

hookformresetuncontrolled

Keeps custom controls aligned with native form reset semantics without reporting uncontrolled resets as user changes.

tsx
const inputRef = useRef<HTMLInputElement>(null);const [value, setValue, , resetValue] = useControllableState({  defaultValue,  onChange: props.onChange,});const invalidatePendingReset = useFormReset(inputRef, defaultValue, resetValue);const handleChange = (nextValue: string) => {  invalidatePendingReset();  setValue(nextValue);};

Installation

$pnpm exec dgadd add ui/form-reset
[Installs to]src/hooks/use-form-reset.ts[Item]ui/form-reset

dgadd is not public on npm yet. Until the first release, pack @diffgazer/add from the repository and install that tarball into this app, which is what puts dgadd on pnpm exec.

UI components require Tailwind CSS v4. Local copy mode imports src/styles/styles.css; package mode uses @diffgazer/ui CSS once packages are available.

Parameters

NameTypeDefaultDescription
refrequiredRefObject<HTMLElement | null>Element ref used to find the closest parent form.
resetValuerequiredTValue passed to onReset when the parent form dispatches a native reset event.
onResetrequired(value: T) => voidCalled with resetValue when the parent form resets.
isUncontrolledbooleantrueSet to false for controlled components. Controlled reset behavior can be provided separately.
controlled{ syncResetBaseline: () => void; onReset: () => void } | undefinedOptional handlers for custom controls that mirror native form state while externally controlled.

Returns

() => voidInvalidates a queued reset before a later user or programmatic value mutation.

Examples

Resettable Input

Preview

Notes

Uncontrolled custom controls

Pair this hook with useControllableState's resetValue setter. It restores the default internal value without calling the public onChange callback.

Latest reset value

The reset listener stays stable while reading the latest resetValue and onReset callback.

Last mutation wins

Call the returned invalidation function immediately before changing the control value so an older queued reset cannot overwrite the newer mutation.

Source

Highlighted source loads after this disclosure opens. Browse the source repository.