-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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] Fix a bug causing a too coarse dependency suggestion #19313
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 5811c92:
|
I'm using the latest version of this plugin, but I think this might still be a problem in some scenarios: This code sandbox: https://codesandbox.io/s/cocky-wind-0kumj?file=/src/App.js still reports an error of a missing |
The way your code is currently structured could cause a problem: const onExit = React.useCallback(() => {
// Because you're reading `onExit` off of an object,
// that object may have been mutated since the last render.
props.onExit("some stuff");
}, [props.onExit]); The lint rule is trying to tell you to do something like this to avoid that problem: const { onExit: onExitProp } = props;
const onExit = React.useCallback(() => {
// Because you're reading a local variable,
// it is guaranteed not to have changed since the last render.
onExitProp("some stuff");
}, [onExitProp]); |
You're getting this because you're calling a function: props.onExit() In JavaScript, this is equivalent to: props.onExit.call(props) In other words, you're passing The recommended fix is to use destructuring, as the message says: export default function App({ onExit }) {
const handleExit = React.useCallback(() => onExit("some stuff"), [onExit]);
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<button onClick={handleExit}>Exit</button>
<h2>Start editing to see some magic happen!</h2>
</div>
);
} https://codesandbox.io/s/wandering-wood-soc63?file=/src/App.js:51-364 |
That explanation makes sense, and it explains why I'm getting this warning for callback props only, and not for reading simple value props. I do question how often the difference between calling |
Fixes #19312.
If we use
props.something
and declareprops.something
in deps, this should not violate.However, the existing logic for AssignmentExpressions caused it to be violating. This is because we forgot to check whether an expression is on the left or the right side. That logic should only have been active for the left side.
Added regression tests.