question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FormContext setError and clearError useEffect cause infinite renders

See original GitHub issue

Description

Using setError, and clearError from useFormContext in the dependency array of a useEffect hooks causes components to infinitely re-render.

I discovered this because we have the react-hooks/exhaustive-deps eslint rule enabled in our codebase, so anything external to the effect MUST be included in the dependency array.

I believe this is happening because useFormContext is providing new copies of clearError and setError on every render. I believe this could be fixed by utilizing useCallback.

Simple example (full codesandbox with usecase included in reproduction steps below)

const { setError, clearError } = useFormContext();

useEffect(() => {
  if(error) {
      setError(error)
  } else {
      clearError()
  }
}, [setError, clearError, error]);

// infinitely renders

I was able to work around this for now by disabling the eslint rule for that line, but this should also be fixed for performance reasons.

I’ll take a look at making PR later today as well, but wanted to get this up first for feedback.

To Reproduce

Steps to reproduce the behavior:

  1. Go to CodeSandbox
  2. Navigate to the DatePicker.js file
  3. Notice how the form works and reports errors as usual, and renders only a few times (reported in console)
  4. Change the useEffect dependency array on line 58 from WORKING_DEP_ARRAY, to INFINITE_RENDER_DEP_ARRAY.
  5. Notice how the form infinitely renders.

Expected behavior

A component should not re-render infinitely if setError, and clearError need to be used within a useEffect dependency array. useFormContext should not return new copies of these functions every time.

Desktop

  • OS: Mac OX
  • Browser: Chrome
  • 77.0.3865.90

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
tknickmancommented, Sep 28, 2019

Boom, that works great. Thanks a ton @bluebill1049!

Updated the sandbox with the beta here and all is well: https://codesandbox.io/embed/react-hook-form-form-context-bug-8nn6w

I’ll close this issue, and I’ll keep using the beta for now and report back if I find anything weird.

Thanks again!

2reactions
bluebill1049commented, Sep 27, 2019

3.23.13-beta.1 i have tested and it’s working

Read more comments on GitHub >

github_iconTop Results From Across the Web

useFieldArray - Simple React forms validation
Performant, flexible and extensible forms with easy-to-use validation.
Read more >
react-hook-form: Versions - Openbase
Reason : Some users may want to use FormContext.Consumer . import { FormContext } from 'react-hook-form'; // in your connected component render() {...
Read more >
“how to use error in react hook form” Code Answer
Infinite loop useEffect · Text with prop value is rendered The component renders variable text based on a string prop. We test that...
Read more >
react-hook-form - FormContext setError et clearError useEffect ...
React-hook-form: FormContext setError et clearError useEffect provoquent des rendus infinis ... [setError, clearError, error]); // infinitely renders.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found