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.

[eslint-plugin-react-hooks] Add option to require functions from core hooks in dependencies

See original GitHub issue

Do you want to request a feature or report a bug?

feature

What is the current behavior?

The react-hooks/exhaustive-deps will currently accept either (and fix neither) of the following:

// Scenario A: Function returned by core hook IS NOT specified as a dependency.
const [value, setValue] = useState(initialValue);
const toggle = useCallback(() => setValue(v => !v), []);
// Scenario B: Function returned by core hook IS specified as a dependency.
const [value, setValue] = useState(initialValue);
const toggle = useCallback(() => setValue(v => !v), [setValue]);

What is the expected behavior?

If we add the following to our .eslintrc.js

// Introduces a `requireCoreFunctions` config option.
'react-hooks/exhaustive-deps': ['error', { requireCoreFunctions: true }]

then the rule should fail in scenario A, and fixing should result in scenario B.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

n/a

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
decafdenniscommented, Mar 21, 2019

TLDR; it would eliminate a gray area for those who want to be more strict and consistent with their code standards.

With regards to including setState/dispatch as hook inputs/dependencies, you’ve been saying something to the effect of “you don’t have to, but it doesn’t hurt.” (Correct me if I’m wrong!) That makes sense, but leaves the developer with a decision to make and an opportunity to bikeshed.

We would prefer to put this in our code standards, and have the linter enforce it:

If you do specify a deps parameter, specify all dependencies, including functions returned from core hooks.

0reactions
stale[bot]commented, Jan 17, 2020

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

eslint-plugin-react-hooks - npm
This option accepts a regex to match the names of custom Hooks that have dependencies. We suggest to use this option very sparingly,...
Read more >
Hooks FAQ - React
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. This page...
Read more >
eslint-plugin-react-hooks | Yarn - Package Manager
Fast, reliable, and secure dependency management. ... ESLint rules for React Hooks ... Declarative: React makes it painless to create interactive UIs.
Read more >
12 essential ESLint rules for React - LogRocket Blog
This rule enforces certain rules about the contents of the dependency array that is passed to Hooks, such as useEffect , useCallback ,...
Read more >
szhsin/eslint-plugin-react-hooks-addons - GitHub
eslint -plugin-react-hooks is awesome for linting the dependency array of React Hooks. But it doesn't do one thing: unused dependencies in the useEffect...
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