-
Notifications
You must be signed in to change notification settings - Fork 47.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
eslint-plugin-react-hooks should report errors inside unnamed functions #14404
Comments
I wasn't able to reproduce this in codesandbox (ie - it shows as a lint error correctly - https://codesandbox.io/s/n4pjo8xvjj). It also appears you've set up eslint with this rule
I'm not super familiar with eslint config options, but doesn't "never" mean the rule is disabled? |
Yeah, you should try it with config from documentation: |
See below, and changed it to "error" as per the npm package doc for hooks, still doesn't work on windows 10 OS. And this is what is in my lint config: |
Dumb question: Did you run npm install? |
Yes and did it now to be sure: "npm install" and "npm run lint". |
I just cloned the repo to Ubuntu and ran "npm install" and "npm run lint". Also shows no error from eslint. |
Sorry, you provided one! I missed it. |
Seems like it doesn't work with a definition like this: export default () => {
if (condition) {
useState()
}
} or like this: export default function() {
if (condition) {
useState()
}
} but works with a named function: export default function App() {
if (condition) {
useState()
}
} We strongly encourage you to give name to your components (so that they show up in DevTools and warnings properly). But we should probably fix this. |
Great catch! |
The linter as of today is looking at the name of the function to check if it a component or a custom hook. |
I just stumbled upon this too. And changing the anonymous function to a named one indeed made the linter find the hooks issue i was expecting it to find. TIL: Name components and custom hooks that I |
Yes it is a common practice which is why a naming components rule belongs in the linter. |
The same problem arises when you wrap a component in This is caught as an error:
but this isn't:
The workaround is to declare it as follows:
|
Ah, ok... I'm happy to have found this thread. I was confused! https://stackoverflow.com/questions/56605578/how-can-i-get-eslint-plugin-react-hooks-to-lint-functional-components-that-are I'll just add names to my |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution. |
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! |
Here's a PR with tests for these cases and others #28147 Related issues:
|
Two more cases I found: Component returned from a factory: function createView() {
return () => {
if (Math.random() > 0.5) {
return null;
}
React.useEffect(() => {}, []); // should error but doesn't
return <div/>;
} Functional component as a field of class: class Foo {
private readonly View = () => {
if (Math.random() > 0.5) {
return null;
}
React.useEffect(() => {}, []); // should error but doesn't
return <div />;
};
} |
I want to report a bug for the hooks plugin.
What is the current behavior?
There was no error report after running eslint, but the component failed when running in the browser.
From the chrome dev console it reported "Uncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement."
**If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React.
Here is a link to the github repo:
https://github.com/paboulos/react-hooks-eslint
What is the expected behavior?
Followed The Hooks API guide which says React hooks provides a linter plugin to enforce these rules automatically.Therefore it should have reported a usage violation when the eslint hooks plugin is specified.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
Using window 10 OS and Chrome browser.
"babel-eslint": "9.0.0",
"babel-loader": "8.0.4",
"eslint": "5.9.0",
"eslint-config-airbnb": "17.1.0",
"eslint-loader": "2.1.1",
"eslint-plugin-import": "2.14.0",
"eslint-plugin-jsx-a11y": "6.1.2",
"eslint-plugin-react": "7.11.1",
"eslint-plugin-react-hooks": "0.0.0"
Then ran package script lint as follows: "npm run lint"
no errors reported.
Then ran package script start as follows: "npm start"
The React component CountHooks calls useState incorrectly and reports error in the browser dev console.
The text was updated successfully, but these errors were encountered: