forge42dev / remix-hook-form

Open source wrapper for react-hook-form aimed at Remix.run
MIT License
330 stars 27 forks source link

Fix extra re-renders caused by formState changes #86

Closed ekerik220 closed 5 months ago

ekerik220 commented 6 months ago

Description

Fixes extra full form re-renders that don't happen when using react-hook-form. Specifically, re-renders that happen because of formState changes, even when the user of the hook isn't watching formState values.

Below is a simple example showing that the entire form is being re-rendered on every key stroke into an input. This is happening because the validation mode is set to onChange and the formState.isValidating flag is changing on every key stroke (note that I haven't accessed formState.isValidating in the component, so the expected behavior is that the component does not re-render based on it).

const {
  register,
  handleSubmit,
  formState: { errors },
} = useRemixForm<FormData>({
  resolver,
  mode: "onChange",
});

https://github.com/Code-Forge-Net/remix-hook-form/assets/5779535/5f53b399-c8a7-4029-9bee-36f099a3ccbc

Type of change

How Has This Been Tested?

After the changes:

https://github.com/Code-Forge-Net/remix-hook-form/assets/5779535/ed7035bb-145b-4b64-8285-d047d0c05cc4

Checklist:

AdiRishi commented 6 months ago

ngl I learned a lot by reading this PR, didn't know you could smartly wrap object properties in functions like that to get around re-renders!