I have a linting error (react-hooks/exhaustive-deps
) that I am currently disabling on two lines. I understand how the code is breaking the rule, but I do not understand why this rule applies to this situation.
I want an effect to only run for cleanUp purposes when the component unmounts. I have a function, clearMessages
, which is inherited from props which reset the state of a reducer back to its empty default state. It works fine when I run my project locally, but when I run the build react-scripts throws the above linting error and the build fails.
Here is a short snippet showing the effect that causes the problem.
const Search = ({ clearMessages }) => {
useEffect(() => () => clearMessages(), [])
...
...
}
This is the error message that react-scripts build throws.
Line 25: React Hook useEffect has a missing dependency: 'clearMessages'. Either include it or remove the dependency array. If 'clearMessages' changes too often, find the parent component that defines it and wrap that definition in useCallback react-hooks/exhaustive-deps
I would not expect clearMessages
to change, so I am not sure why it is important that I provide it as a dependency. I do not want the effect to the only run when the value of clearMessages
changes.