-
Notifications
You must be signed in to change notification settings - Fork 1.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
fix: sync mutations are applied independently #1003
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 c18af79:
|
Thanks for looking into this and creating a minimal reproduction in #994 (comment)! I'm a little bit worried that with this change, SWR no longer batches synchronous mutations and components will render intermediate result, which is another issue that we don't want to have (we can't ask users to always use However I'm kinda 50/50 on this and it might be a good thing. Since React itself doesn't batch the results by default if you do 2 |
I just verified that with this fix, intermediate results will not be rendered: https://codesandbox.io/s/swr-basic-forked-d78ie?file=/src/App.js (it won't log any odd numbers). It seems like React deduped them automatically, without Anyways this is a good fix, thank you very much! |
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 998c0d8:
|
I agree with you and I have been hoping
Yeah, this is the default behavior of React, so it makes sense for me than optimizing the update with undesirable edge cases.
React does batching updates in an event handler by default, so the updates in the CodeSandbox are batched. |
Thank you! |
Thank you! |
fixes #994.
A codesandbox in #994 now works correctly. https://codesandbox.io/s/swr-basic-forked-qrbpc?file=/src/App.js
This is the same approach in #735, so this causes unnecessary renders. But React batches updates in an event hander and has
unstable_batchedUpdates
for the API, which means developers can make those updates in a batched way.On the other hand, the problem described in #994 is hard to avoid for developers and seems to be a more common case, so I think it should be fixed.