Bug: [eslint-plugin-react-hooks] exhaustive-deps false positive on "unnecessary" dependency if its a React component
See original GitHub issueSteps to reproduce
- create a memoized value using
useMemo
- a React component is used in the creation of this value, in a JSX expression
- specify the React component in the dependency array
Link to code example: https://github.com/zeorin/eslint-plugin-react-hooks-repro
The current behavior
React Hook useMemo has an unnecessary dependency: 'Component'. Either exclude it or remove the dependency array react-hooks/exhaustive-deps
The expected behavior
No lint errors.
More details
A simple repro (taken from the link above) is:
function Foo({ component: Component }) {
const memoized = useMemo(() => ({
render: () => <Component />
}), [Component]);
return memoized.render();
}
Workarounds
If one changes the component to lowercase, the lint error goes away. It does also mean that we need to change the way we render the component:
function Foo({ component }) {
const memoized = useMemo(() => ({
render: component
}), [component]);
return memoized.render();
}
Alternatively we can decide not to use JSX, in which case the lint rule functions correctly, too:
function Foo({ component: Component }) {
const memoized = useMemo(() => ({
render: () => React.createElement(Component)
}), [Component]);
return memoized.render();
}
Impact
Currently it is hard to use props that are components in a JSX expression if one is using the exhaustive-deps
rule.
This is also compounded by the fact that this rule has a ESLint fix that removes the dependency, thus changing the behaviour of the code and leading to bugs. See https://github.com/facebook/react/issues/16313 for that bug report.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:10 (6 by maintainers)
Top Results From Across the Web
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 >Removing Effect Dependencies - React Docs
Follow this guide to review and remove unnecessary dependencies from your Effects. You will learn. How to fix infinite Effect dependency loops; What...
Read more >eslint-plugin-react-hooks shows incorrect "missing dependency"
Such as the case where a setState function inside a custom Hook gets returned to your component, and then you call it from...
Read more >eslint-plugin-react-hooks | Yarn - Package Manager
Design simple views for each state in your application, and React will efficiently update and render just the right components when your data...
Read more >A Complete Guide to useEffect - Overreacted
Effects are a part of your data flow. ... Whenever we update the state, React calls our component. Each render result “sees” its...
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
I am interested in looking into a fix, I had a look but I think there’s something implicit that I’m missing. I’d appreciate a few pointers.
Great, thank you very much @eps1lon !