-
Notifications
You must be signed in to change notification settings - Fork 5
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
Do not inject setState/ref into dependencies #10
Comments
This is a very nice suggestion! We could also exclude:
|
Thanks for the response! |
function MyComponent() {
const [id] = React.useState(() => Math.random());
// Doesn’t require [id] since it will never change
useAutoEffect(() => {
console.log(id);
});
const [counter, setCounter] = React.useState(0);
// Again, `setCounter` is not used, `counter` will never change!
const values = useAutoMemo(new Array(counter));
const handleLog = React.useCallback(() => {}, []);
// `handleLog` will (*should*) never change, using as a dependency is reduntant
useAutoEffect(() => handleLog('Hallo!'));
} |
Found the relative code in the ESLint exhaustive-deps rules: https://github.com/facebook/react/blob/fe2cb525542443aaf1447c4069354c12659fd186/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js#L296 Should be straight forward to add them to this macro. |
@Glinkis you can see this implemented in v1.1.0. Should bring those small perf improvements :) |
Thanks! |
Currently testing this macro, so far with great success!
But I have one minor nitpick.
Currently this:
Is converted to:
But adding
setState
to the dependency array is redundant, since react guarantees it to be referentially stable. The same is true foruseRef
.The
react-hooks/exhaustive-deps
eslint takes both of them into consideration.The text was updated successfully, but these errors were encountered: