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

fix: sync mutations are applied independently #1003

Merged
merged 7 commits into from
Mar 4, 2021

Conversation

koba04
Copy link
Collaborator

@koba04 koba04 commented Mar 1, 2021

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.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 1, 2021

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:

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration
eloquent-river-m341r Issue #994

@shuding
Copy link
Member

shuding commented Mar 1, 2021

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 unstable_batchedUpdates, and that API isn't officially stable).

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 setStates in a row. So like you said, this should be something that developers should handle.

@shuding
Copy link
Member

shuding commented Mar 1, 2021

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 unstable_batchedUpdates?

Anyways this is a good fix, thank you very much!

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 2, 2021

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:

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration
SWR-Basic (forked) PR
agitated-heyrovsky-iw4hf Issue #994

@koba04
Copy link
Collaborator Author

koba04 commented Mar 2, 2021

@shuding

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 unstable_batchedUpdates, and that API isn't officially stable).

I agree with you and I have been hoping unstable_batchedUpdates would be a stable API over five years 😭
Actually, I don't have enough confident for this change yet, so I would need more investigations.

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 setStates in a row. So like you said, this should be something that developers should handle.

Yeah, this is the default behavior of React, so it makes sense for me than optimizing the update with undesirable edge cases.

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 unstable_batchedUpdates?

React does batching updates in an event handler by default, so the updates in the CodeSandbox are batched.

@koba04 koba04 marked this pull request as ready for review March 2, 2021 15:05
@shuding
Copy link
Member

shuding commented Mar 4, 2021

Thank you!

@shuding shuding merged commit 5a15f4b into vercel:master Mar 4, 2021
@koba04 koba04 deleted the fix-sync-mutation branch March 4, 2021 14:16
@koba04
Copy link
Collaborator Author

koba04 commented Mar 4, 2021

Thank you!

This was referenced Mar 15, 2021
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.

Mutating state via onChange causes cursor jump to end of input
2 participants