Bug: eslint-react-hooks asking (exhaustive-deps) asking for the full object instead of the property that is being used on useEffect
See original GitHub issueReact version:
"react": "^16.13.1",
"react-dom": "^16.13.1",
devDependencies
"eslint": "^7.0.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-module-utils": "^2.6.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-react-hooks": "^4.0.0",
Steps To Reproduce
This code:
function MyComponent() {
const lastPathname_ref = useRef(null);
const location = {pathname: ""};
useEffect(() => {
lastPathname_ref.current = location.pathname;
},[]);
}
Link to code example:
https://codesandbox.io/s/solitary-currying-htsp3
The current behavior
This very same code is asking for different variables in the dependency array:
On CodeSandbox, it’s asking for location.pathname
, which is correct and is the intended behavior of my useEffect
.
On my local environment (using the versions I’ve mentioned above), it’s asking for the full location
object. Which I think it’s not correct, and that is not what I need.
I don’t know what versions the CodeSandbox environment is using.
The expected behavior
It should be asking for the location.pathname
property, instead of the full location
object.
NOTE
This issue seems to be different than https://github.com/facebook/react/issues/16265 , because that one is caused when you call a method straight from the object, without destructuring it first. This issue is happening when using a property, not a method.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:12 (1 by maintainers)
I also have this behavior since upgrading to “eslint-plugin-react-hooks”: “4.0.2”, from version 4.0.0.
In my case:
Produce the following warning message:
Seems like this issue is still here.