onChange runs before the inputs show the new value
Detect
- An
onChangehandler in your code that reads input values from the DOM, such asdocument.querySelector('#root_title').value. - An
onChangehandler that measures or focuses inputs and expects them to show the edit. - A
FormRefcall, such assetFieldValue()orreset(), wrapped instartTransition().
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
onChangesaves matches the value the user just entered, not the one before it. - No
onChangehandler reads input values from the DOM. - No
FormRefcall is wrapped instartTransition().
Also read: