liveBook · Manning

formResetCallback

As explained in Web Component Development with Modern Libraries and Tooling

A form-associated custom-element lifecycle callback invoked when the owning form is reset.

formResetCallback is a lifecycle callback for form-associated custom elements. The browser invokes it when the owning form is reset, either by a reset button or by calling the form's reset() method. Its job is to restore the component's state to its initial state. section 3.2

Resetting a custom radio group

A radio-group component can save its starting value when it becomes associated with a form, then restore that value during reset: formAssociatedCallback() { this.#initialValue = this.value; } formResetCallback() { this.value = this.#initialValue; } With static formAssociated = true and an element nested inside a form, clicking Reset restores the initial selection. Assigning through value also updates the form value and the checked radio button, so the reset remains synchronized with the component's UI and submission state. section 3.10 section 3.2

Use the callback with these conditions:

  • Set static formAssociated = true and place the custom element inside a form. section 3.2
  • Capture the initial state in formAssociatedCallback(), which runs when the associated component is added to the DOM. section 3.2
  • Restore state in formResetCallback() rather than only clearing internal fields; the component must also update its rendered controls and submitted form value. section 3.10

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Defines invocation requirements and demonstrates initial-state storage plus restoration, without detailing broader form-associated internals.
Examples
Uses a radio-group component whose selected value is saved initially and restored after a form reset.
What it assumes
Assumes custom elements, HTMLElement subclasses, and forms; establishes the form-association conditions needed for the callback.
What you can do after
Build it from scratch by storing initial component state and restoring it through the value property during form resets.
How it connects
Connects form-associated custom elements with formAssociatedCallback, custom element lifecycle callbacks, form reset(), and form submission state.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Component Development with Modern Libraries and Tooling. The section links point to the parts of the book it draws on.