Skip to content

Commit

Permalink
fix: prevent unnecessary renders when form has errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Sep 26, 2023
1 parent f80d67c commit 5c85959
Showing 1 changed file with 17 additions and 11 deletions.
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 */
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

0 comments on commit 5c85959

Please sign in to comment.