-
Notifications
You must be signed in to change notification settings - Fork 47k
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] warn on unexpanded object dependencies #24636
[eslint-plugin-react-hooks] warn on unexpanded object dependencies #24636
Conversation
Comparing: a276638...08edbed Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: Expand to show
|
Upon further testing I've found that a primitive derived from a destructuring assignment is tripping the |
Thanks, @gnowland for creating this PR! Bumping this up, as I had an issue with a similar thing. When there is a spread operator used ESLint should report the error as this dependency will change on every render.
I've ended up adding an additional check to https://github.com/facebook/react/blob/main/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js#L1532
but I would love to have the option of avoiding all the objects in hooks dependencies. |
This should be a default rule. We just had a freeze in one of the main pages of our product because an object dependency in an effect triggered a (never ending) feedback loop... 😒 Thank you for creating the PR, hope it gets merged soon! |
This is a good idea, but I found that as-is, it quickly runs into problems of mis-labeling things in a way that makes it useless in a large codebase. For example: const [doTheThing, setDoTheThing] = useState(false);
const memoResult = useMemo(() => {
// some result here that changes based on doTheThing
}, [doTheThing]) // this is an error |
This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated. |
Would still be great to have IMO... |
This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated. |
Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you! |
This is still an issue, but commenting was disallowed, so did not have an option to "bump" |
Summary
This PR expands upon the existing logic which reports functions and constructors defined inside the render function as invalid hook dependencies (#19590) to allowing a developer to receive warning about any object present in a dependencies array when an
avoidObjects
option is set totrue
in theexhaustive-deps
rule.This is necessary because current rules do not cover the case where an object that will be referentially unique on each render is passed into the component as a parameter and used as a dependency, only objects created within the component.
It has been my experience in code reviews that the referential inequality of objects causing side-effects to execute unnecessarily is a particularly common cause of unnecessary memory use, especially when combined with XHR requests (a common useEffect action). Confoundingly There is no mention of object comparison by referential equality or warning thereto in the React Handbook Hooks API Reference.
This PR aims to provide a flexible way for developers to opt-in to receiving a detailed message regarding potential unintended side effects arising from the use of an object in a dependency array, and tips to resolve the issue (by destructuring or using property accessors - not deep equality checks as Dan Abramov is on the record against them).
This is also a feature that has been requested elsewhere.
How did you test this change?
I created a derivative implementation, used it in a couple of existing React projects, and wrote tests and ran
yarn test
in this repo.