Skip to main content
Version: Current (v7.0.0-beta.3)

onChange runs before the inputs show the new value

Detect​

  • An onChange handler in your code that reads input values from the DOM, such as document.querySelector('#root_title').value.
  • An onChange handler that measures or focuses inputs and expects them to show the edit.
  • A FormRef call, such as setFieldValue() or reset(), wrapped in startTransition().

Change​

onChange now runs before the inputs show the new value. Take the value from the event:

const onChange = (event) => {
- const title = document.querySelector('#root_title').value;
- saveDraft({ title });
+ saveDraft(event.formData);
};

Outside onChange, call getFormData() on the form ref (see the FormRef type). It returns the form's current value, including edits made earlier in the same tick. Code that needs the updated DOM itself belongs in an Effect.

Form stores edits synchronously, so a FormRef call wrapped in startTransition() still runs at once and doesn't become non-blocking. Remove the wrapper:

-startTransition(() => formRef.current?.setFieldValue('title', 'Draft'));
+formRef.current?.setFieldValue('title', 'Draft');

Verify​

  • What your onChange saves matches the value the user just entered, not the one before it.
  • No onChange handler reads input values from the DOM.
  • No FormRef call is wrapped in startTransition().

Also read: