A ref to Form holds a FormRef handle, not the class instance
Detect
useRef<Form<orcreateRef<Form<in your code.FormStateimported from@rjsf/corein your code..state.formData,.state.errorSchema,.state.errors,.state.schemaUtils,.state.registry,.setState(or.formElementread through aFormref in your code.- Type checker:
Property 'state' does not exist on type 'FormRef<MyData>'.(TS2339), or the same forsetStateorformElement, andModule '"@rjsf/core"' has no exported member 'FormState'.(TS2614).
Change
Form is now a function component. Its ref exposes FormRef<T> instead of a class instance. FormState, the class component's state type, is no longer exported. IChangeEvent is the public shape of what reaches your callbacks.
First, change the ref's type. Use your form-data type in place of MyData:
-import Form from '@rjsf/core';
+import Form, { type FormRef } from '@rjsf/core';
-const formRef = useRef<Form<MyData>>(null);
+const formRef = useRef<FormRef<MyData>>(null);
The same change applies to createRef. Then replace any access to class internals:
| If your code uses… | Change it to… |
|---|---|
formRef.current.state.formData | formRef.current.getFormData() |
.state.errorSchema or .state.errors | Read event.errorSchema or event.errors in onChange/onSubmit. |
.setState(...) | Call .setFieldValue(path, value), or .setFieldValue('', data) to replace the whole value. |
.state.schemaUtils | Use registry.schemaUtils in a custom field, or create one with createSchemaUtils({ validator }, schema). |
.state.registry | Use the registry prop in a custom field, widget or template. Outside Form, use buildRegistry(props, schemaUtils.getRootSchema(), schemaUtils). |
.formElement | Use .reset() if you need to reset the form. The handle does not expose the DOM element. |
For example, save the data using the handle and keep error information from events:
-const data = formRef.current.state.formData;
+const data = formRef.current.getFormData();
-const errors = formRef.current.state.errors;
+const onChange = (event) => setErrors(event.errors);
submit(), setFieldValue(), validateForm(), validateFormWithFormData() and focusOnError() keep their existing signatures. reset() takes an optional seed, see reset() goes back to the seed. validate() changed separately, see FormRef.validate() takes only the data.
Verify
- The type checker no longer reports TS2339 for
state,setStateorformElementonFormRef, or TS2614 forFormState. - No code reads
.state,.setStateor.formElementthrough the ref. - Data you save through the ref matches what the form shows.
- Error lists you kept from
.state.errorsnow update from youronChangeoronSubmithandler.
Also read: