Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent unnecessary renders when form has errors #358

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/data-workspace/entry-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ export const EntryForm = React.memo(function EntryForm({ dataSet }) {
lockStatus: { state: lockState },
} = useLockedContext()
const formType = dataSet.formType
const setFormErrors = useEntryFormStore((state) => state.setErrors)

useFormState({
onChange: (formState) => {
setFormErrors(formState.errors)
},
subscription: {
errors: true,
},
})

const Component = formTypeToComponent[formType]

Expand All @@ -72,12 +62,28 @@ export const EntryForm = React.memo(function EntryForm({ dataSet }) {
formType={formType}
/>
)}

<EntryFormErrorSpy />
<Component dataSet={dataSet} globalFilterText={globalFilterText} />
</>
)
})

/* Used to sync store with errors from form
In its own component to prevent unecessarily re-renders in the tree */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Used to sync store with errors from form
In its own component to prevent unecessarily re-renders in the tree */
/**
* Used to sync global store with errors from final-form.
* Because of the `errors` subscription in `useFormState`, this re-renders
* on every form input change if there is an error in the form. Therefore, this
* has its own component to prevent unnecessary re-renders below it in the tree
*/

const EntryFormErrorSpy = () => {
const setFormErrors = useEntryFormStore((state) => state.setErrors)

useFormState({
onChange: (formState) => {
setFormErrors(formState.errors)
},
subscription: {
errors: true,
},
})
return null
}

EntryForm.propTypes = {
dataSet: PropTypes.shape({
displayName: PropTypes.string,
Expand Down