π `useExhaustiveDependencies` error's when (adding and not adding) to array
mittalyashu opened this issue Β· comments
Environment information
I am getting this error `This hook does not specify all of its dependencies: toggleSidebar` from a component, being used inside `useMemo`
{useMemo(
() => (
<SideDrawer
closeSideDrawer={() => toggleSidebar(null)}
/>
),
[drawer],
)}
If I add `toggleSidebar` to the dependency array, the above error goes away.
But a new error pops in, and says.
`toggleSidebar changes on every re-render and should not be used as a hook dependency.`
Rule name
lint/correctness/useExhaustiveDependencies
Playground link
Expected result
NIL
Code of Conduct
- I agree to follow Biome's Code of Conduct
Wasn't exactly sure, what should be the issue title.
This is a react usage problem. Please search for relative knowledge:
- https://stackoverflow.com/questions/56492261/the-function-makes-the-dependencies-of-useeffect-hook
- https://dev.to/amitaldo/function-makes-the-dependencies-of-useeffect-hook-change-on-every-render-warning-in-react-35hd
In your case, you should wrap your function in a useCallback
hook or define it outside of the component.
For reference, there was a similar issue reported last week: #3057