Bug: eslint-plugin-react-hooks: Cannot read property 'references' of undefined
See original GitHub issueeslint-plugin-react-hooks is throwing a Cannot read property 'references' of undefined
error on /node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:1683:23
I added a console log statement before that line
var usedDep = dependencies.get(missingDep);
console.log('usedDep', usedDep, missingDep, dependencies)
if (usedDep.references[0].resolved !== topScopeRef) { // error is thrown here
and this is the output.
usedDep undefined program?.slug Map {
'program?.uuid' => { isStatic: false, references: [ [Reference] ] },
'monitorService' => { isStatic: false, references: [ [Reference] ] },
'navigate' => { isStatic: false, references: [ [Reference] ] },
'program.slug' => { isStatic: false, references: [ [Reference] ] } }
I don’t actually have program?.slug
referenced anywhere in this file.
I am using program.slug
though.
Versions: eslint-plugin-react-hooks@4.0.4 eslint@6.8.0 babel-eslint@10.1.0
Steps To Reproduce
The code below throws the error. I have found 2 ways to stop the error from happening.
- Removing the line
programUuid: program?.uuid ?? '',
- adding
program.slug
to the list of dependencies.
Link to code example:
const onSave = useCallback(
args => {
const request: CreateMonitorV3Request = {
programUuid: program?.uuid ?? '',
};
monitorService
.create(request)
.then(x => {
// $FlowFixMe - program will be available
navigate(`/programs/${program.slug}/monitors/${x.uuid}`);
})
.catch(ex => {
console.log('error', ex);
});
},
[monitorService, program?.uuid]
);
The current behavior
Error is thrown Cannot read property 'references' of undefined
The expected behavior
eslint successfully reports that I’m missing dependencies in useCallback
Full Stack trace:
TypeError: Cannot read property 'references' of undefined
Occurred while linting /project-root/src/pages/monitor/configuration-pages/new-monitor-page.js:26
at /project-root/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:1683:23
at Set.forEach (<anonymous>)
at visitFunctionWithDependencies (/project-root/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:1674:29)
at visitCallExpression (/project-root/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:826:11)
at listeners.(anonymous function).forEach.listener (/project-root/node_modules/eslint/lib/linter/safe-emitter.js:45:58)
at Array.forEach (<anonymous>)
at Object.emit (/project-root/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
at NodeEventGenerator.applySelector (/project-root/node_modules/eslint/lib/linter/node-event-generator.js:254:26)
at NodeEventGenerator.applySelectors (/project-root/node_modules/eslint/lib/linter/node-event-generator.js:283:22)
at NodeEventGenerator.enterNode (/project-root/node_modules/eslint/lib/linter/node-event-generator.js:297:14)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:12 (4 by maintainers)
Top Results From Across the Web
Cannot read property 'references' of undefined in eslint-plugin ...
Certain code patterns using optional chaining syntax causes eslint-plugin-react-hooks to throw an error. React version: 16.10.2 Steps To ...
Read more >Cannot read property 'name' of undefined Occurred while ...
Ok so I solved the problem. For some reason the prefer-csf rule of eslint-plugin-storybook causes this error.
Read more >ESLint error in react when using useEffect : WEB-49618
Open the file: Hooks.js. TypeError: Cannot read property 'type' of undefined Occurred while linting /Users/kristina.Pchelintseva/WebstormProjects/ ...
Read more >Rules of Hooks - React
We released an ESLint plugin called eslint-plugin-react-hooks that enforces these two rules. You can add this plugin to your project if you'd like...
Read more >cannot read properties of null usecontext - You.com | The AI ...
Store isn't a React component so it can't use the useStore hook. useStore. This hook returns a reference to the same Redux store...
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
Thanks to @coolkev I just updated #19062 to include a fix for this issue.
PR #19062 was supposed to fix this issue. It was merged and released in 4.0.5 but the issue was accidentally left open.
If folks are seeing a similar bug, please file a new issue with repro information. I’m going to close this one.