[eslint-plugin-react-hooks] Bug: exhaustive-deps not working with function declaration
See original GitHub issueWhen extracting a useEffect
callback from inline to a funcion declaration, the exhaustive-deps
rule stops showing warnings related to the referenced function
React version: 16.12.0
eslint-plugin-react-hooks version: 1.7.0
Steps To Reproduce
- Add a basic
useEffect
code which doesn’t declare one of the dependencies in the array (tmp
)
const [tmp, setTmp] = useState("");
useEffect(() => {
console.log(tmp);
}, []);
- ESLint shows the
exhaustive-deps
warning - Extract the
useEffect
callback to a function declaration
const [tmp, setTmp] = useState("");
function sideEffect() {
console.log(tmp);
}
useEffect(sideEffect, []);
Link to code example: https://codesandbox.io/s/summer-sunset-oie9s
The current behavior
ESLint plugin does not show the exhaustive-deps
warning
The expected behavior
ESLint plugin should work even if the useEffect
callback isn’t inline
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Definition for rule 'react-hooks/exhaustive-deps' was not found
I referred to this post to fix this but the solution mentioned doesn't work in my case.
Read more >Understanding the React exhaustive-deps linting warning
The first exhaustive deps warning we got was because a single primitive variable was missing in the dependency array. It is pretty simple...
Read more >Hooks FAQ - React
We provide an ESLint plugin that enforces rules of Hooks to avoid bugs. It assumes that any function starting with ” use ”...
Read more >How to manage the useEffect dependency array like a pro?
But we still have some work to have exhaustive dependencies! Declare the function inside the useEffect. If we look at the eslint warning...
Read more >Best Practices with React Hooks - Bits and Pieces
Previously, React features such as state and lifecycle functions are available only for class-based components. Function-based components ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
oops I meant https://github.com/facebook/react/issues/16573
Sorry for deleting the previous comment, wrong account.
The effects weren’t meant to be reused, the goal was to name them, in order to avoid having to read the impl to understand what they do.
I preffered this approach over custom hooks because the code is coupled to the component and is not really reusable.