Form data is deep-frozen in development
Detect
- Code that mutates
event.formDatain anonChangehandler, for exampleevent.formData.name = 'x'. - Code that keeps mutating the object you passed as
initialFormDataorformData, such as v6'sthis.state.data.address.city = 'x'. - The browser console or a test in development: a
TypeErrorsaying a property can't be assigned on a frozen or read-only object.
Change
In development the formData passed to onChange is deep-frozen, so mutating it throws instead of silently corrupting your state. Only plain objects and arrays are frozen. The object you pass the form, initialFormData or formData, is frozen too from the form's first edit, validation, submit or reset on, since the form's data shares it. All of it is frozen except the objects and arrays the form filled a default into, which it copied. Change a copy instead:
const onChange = ({ formData }) => {
- formData.address.city = 'x';
- setData(formData);
+ setData({ ...formData, address: { ...formData.address, city: 'x' } });
};
For retrieved schemas shared with callbacks, see Callback form data and retrieved schemas are shared.
Verify
- No
TypeErrorabout a frozen object in development. - The data you store changes only through copies, and the form shows the values you set.
Also read: