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.

Bug: [eslint-plugin-react-hooks] exhaustive-deps false positive on "unnecessary" dependency if its a React component

See original GitHub issue

Steps to reproduce

  1. create a memoized value using useMemo
  2. a React component is used in the creation of this value, in a JSX expression
  3. 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:open
  • Created 4 years ago
  • Reactions:5
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
zeorincommented, Feb 18, 2020

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.

0reactions
delca85commented, Oct 17, 2020

Great, thank you very much @eps1lon !

Read more comments on GitHub >

github_iconTop 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 >

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