Skip to content

Form Reset

form.reset(), a <button type="reset"> and every other native reset path restore each control to its current default: the last value you passed through its value prop. The controls report nothing, because a reset is not a user change; the one notification is the form’s own reset event, which you already have.

Three rules to build on:

  1. The event arrives before the restore. Inside your reset handler every control still holds its pre-reset value, and a FormData built there carries the pre-reset payload. Restore your own state there; read the form’s restored values only in a later task.
  2. preventDefault() replaces the whole restore. Cancel the event and neither the browser nor the library restores anything: the form is yours.
  3. A separate copy of the value goes stale by design. With bind:value the control puts your variable back along with itself. If you keep the value somewhere else, a reset restores the control and not your copy: put it back in your reset handler. Restore it in a later task and check defaultPrevented, or a handler that runs after yours could cancel the reset and leave your state emptied under an untouched form:
<!-- Svelte -->
<form
onreset={(event) => {
setTimeout(() => {
if (event.defaultPrevented) return;
draft = "";
}, 0);
}}
>
<TextField label="Name" name="name" value="Ada" />
<button type="reset">Reset</button>
</form>
<!-- Vue: the same rule, and v-model is the common case here. A control bound
with v-model puts your ref back along with itself; anything else you
keep is yours to restore. -->
<template>
<form @reset="onReset">
<TextField label="Name" name="name" v-model="name" />
<button type="reset">Reset</button>
</form>
</template>
<script setup lang="ts">
import { ref } from "vue";
const name = ref("Ada");
const draft = ref("");
const onReset = (event: Event) => {
setTimeout(() => {
if (event.defaultPrevented) return;
draft.value = "";
}, 0);
};
</script>

One limit worth knowing: setting the prop to the value the user just typed cannot be told apart from the control giving that value back, so it does not become the new default. Choose a different value first, or reset your own state instead.

The demo above pairs the library form with a native reference: edit both, reset both, and watch the callback counter stay still. The decision and its evidence live in ADR 0012 and docs/form-reset-contract.md.

The event is not the moment to read the form. A FormData built in the handler is the payload the user is resetting away from, and for one task after that the form is mixed: the plain inputs are already restored, the controls that submit through a hidden input are not. Read it in a later task, and it is whole:

Once, though, is not enough. Your handler is on the form and the controls listen on the document, so the browser runs yours first and the timer you queue there runs before theirs. One task later the plain inputs are back and the hidden-input controls are not; the task after that, the form is whole:

form.addEventListener("reset", (event) => {
new FormData(form).get("name"); // the pre-reset value: not the read you want
setTimeout(() => {
// The plain inputs are back here, the composite ones are not yet.
setTimeout(() => {
if (event.defaultPrevented) return;
new FormData(form).get("name"); // restored, every control included
new FormData(form).get("code"); // the pin input too
}, 0);
}, 0);
});

Your own state does not need any of this: restore it in the handler, where a single deferral is enough to see whether the reset was cancelled.

In React this is not optional politeness, it is the whole of your side of the contract. Svelte’s bind:value and Vue’s v-model let a control hand the restored value back to the state that fed it; React has no such channel, so after a reset your state still holds what the user typed, and the next render writes it back over the control. One reset handler that puts your own state back is all it takes, and it is the same handler you would write anyway.

All of them: Svelte, Vue, React and the custom elements. The rule is the same everywhere, whatever the value looks like in that framework: a value handed back to a control that already holds it is an echo, not a new default.

Two adapters ask for one thing extra. In the custom elements the value you set, as an attribute or through the property, is the current default. In React the components do this for you, while the hooks underneath them (useCheckbox, useSwitch) take the element they render on as controlRef: a control that cannot point at an element cannot say which form it belongs to, so a consumer rendering their own markup passes their ref to get the same behaviour.