Skip to content
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

Wrap side effects to fix new warnings in React 16.13 #317

Merged
merged 1 commit into from Mar 30, 2020

Conversation

neolefty
Copy link
Contributor

@neolefty neolefty commented Mar 25, 2020

Fix for #290 "Warning in React 16.13.0"

New warnings in React 16.13 — especially triggering setState() from another component.

This change:

  • Is minimal. It wraps setTimeout() around two setState() calls in useSWRPages(). There's a comment in it that we can probably delete.
  • Silences the new warnings people are seeing in Warning in React 16.13.0 #290 — tested on branch @neolefty/publish and available to try out from @neolefty/swr.
  • Probably adds another rendering roundtrip by delaying setState(). But that's actually the point of the new warnings, I think.

revalidate: swr.revalidate,
isValidating: swr.isValidating,
mutate: swr.mutate
// hoist side effects: setPageSWRs and setPageOffsets -- https://reactjs.org/blog/2020/02/26/react-v16.13.0.html#warnings-for-some-updates-during-render
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
// hoist side effects: setPageSWRs and setPageOffsets -- https://reactjs.org/blog/2020/02/26/react-v16.13.0.html#warnings-for-some-updates-during-render

Okay to delete this comment?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to keep it! :)

@neolefty neolefty changed the title Hoist side effects to fix new warnings in React 16.13 — #290 Hoist side effects to fix new warnings in React 16.13 Mar 25, 2020
isValidating: swr.isValidating,
mutate: swr.mutate
// hoist side effects: setPageSWRs and setPageOffsets -- https://reactjs.org/blog/2020/02/26/react-v16.13.0.html#warnings-for-some-updates-during-render
setTimeout(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another way to hoist is useEffect()s, possibly with dependency arrays to take the place of the if clauses below.

@neolefty neolefty changed the title Hoist side effects to fix new warnings in React 16.13 Wrap side effects to fix new warnings in React 16.13 Mar 25, 2020
Copy link
Member

@shuding shuding left a comment

Choose a reason for hiding this comment

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

Looks good, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants