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

A ref to Form holds a FormRef handle, not the class instance

Detect​

  • useRef<Form< or createRef<Form< in your code.
  • FormState imported from @rjsf/core in your code.
  • .state.formData, .state.errorSchema, .state.errors, .state.schemaUtils, .state.registry, .setState( or .formElement read through a Form ref in your code.
  • Type checker: Property 'state' does not exist on type 'FormRef<MyData>'. (TS2339), or the same for setState or formElement, and Module '"@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.formDataformRef.current.getFormData()
.state.errorSchema or .state.errorsRead event.errorSchema or event.errors in onChange/onSubmit.
.setState(...)Call .setFieldValue(path, value), or .setFieldValue('', data) to replace the whole value.
.state.schemaUtilsUse registry.schemaUtils in a custom field, or create one with createSchemaUtils({ validator }, schema).
.state.registryUse the registry prop in a custom field, widget or template. Outside Form, use buildRegistry(props, schemaUtils.getRootSchema(), schemaUtils).
.formElementUse .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, setState or formElement on FormRef, or TS2614 for FormState.
  • No code reads .state, .setState or .formElement through the ref.
  • Data you save through the ref matches what the form shows.
  • Error lists you kept from .state.errors now update from your onChange or onSubmit handler.

Also read: