Skip to content

Update hooks-effect.md - #3534

Open
lbfalvy wants to merge 2 commits into
reactjs:mainfrom
lbfalvy:patch-1
Open

Update hooks-effect.md#3534
lbfalvy wants to merge 2 commits into
reactjs:mainfrom
lbfalvy:patch-1

Conversation

@lbfalvy

@lbfalvy lbfalvy commented Feb 18, 2021

Copy link
Copy Markdown

Warn about variable-length dependency arrays

The question appeared under react/react#18229, where I also described other merits of this approach.

Warn about variable-length dependency arrays
@facebook-github-bot

Copy link
Copy Markdown
Collaborator

Hi @lbfalvy!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@reactjs-bot

reactjs-bot commented Feb 18, 2021

Copy link
Copy Markdown

Deploy preview for reactjs ready!

Built without sensitive environment variables with commit 3b8bda5

https://deploy-preview-3534--reactjs.netlify.app

@facebook-github-bot

Copy link
Copy Markdown
Collaborator

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

Comment thread content/docs/hooks-effect.md Outdated
>
>We recommend using the [`exhaustive-deps`](https://github.com/facebook/react/issues/14920) rule as part of our [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks#installation) package. It warns when dependencies are specified incorrectly and suggests a fix.
>
>Also note that if you compose the dependencies array dynamically its length will not be checked, but the two arrays will be compared up to the length of the old one. When you pass variable-length arrays in second argument, always prepend the length of the array like so: `[myArray.length, ...myArray]`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the work!

I would rather note that this is not supported. React will explicitly warn:

Warning: The final argument passed to useEffect changed size between renders. The order and size of this array must remain constant.

The behavior about what elements are compared is not mentioned and might just be an implementation detail.

To be sure, I would propose this suggestion as an addition to the warning if this is indeed the recommended fix. The relevant warning is issued here: https://github.com/facebook/react/blob/696e736be11b22e8d4fa40438d235b63db7fddde/packages/react-reconciler/src/ReactFiberHooks.old.js#L331-L339

If we want to recommend an "always" then the warning should include it as well. If there are cases where we don't want to include myArray.length we should describe them here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the comparison logic in React and it appears that it only compares up to the shorter of the two lengths so while my suggestion works the explanation is factually incorrect.

In my rather limited experience with React, hooks being dependent on the indices of a variable length array is a pretty normal phenomenon and I can't think of any other way to handle them beside what I described here, so I think an "always" is warranted.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the comparison logic in React and it appears that it only compares up to the shorter of the two lengths so while my suggestion works the explanation is factually incorrect.

That's fine. But the source code of React does not imply a behavior we can rely on.

so I think an "always" is warranted.

I'm not worried about whether this is warranted. If it is, then this should be part of the warning itself. So to confirm your proposal, please propose this to the React core as well and see what the core maintainers have to say. This will get a response faster.

Fixed description of dependency comparison behavior
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants