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 bug with useMutation shared results #1616

Conversation

Shrugsy
Copy link
Collaborator

@Shrugsy Shrugsy commented Oct 16, 2021

  • Fix a scenario where the component calling reset needed to call it twice when using shared component results

Addresses the scenario described here: #1477 (comment)

A regression test is included.

- Fix a scenario where the component calling `reset`
  needed to call it twice when using shared component results
@Shrugsy Shrugsy requested a review from phryneas October 16, 2021 16:19
@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 16, 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 f2c3606:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration
rsk-github-issues-example Configuration
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration

@Shrugsy
Copy link
Collaborator Author

Shrugsy commented Oct 16, 2021

To test the demos below, try triggering a mutation, then resetting from the same component (i.e. click trigger, then click the reset button next to it)

Before

https://codesandbox.io/s/examples-query-react-basic-forked-185pk?file=/src/App.tsx

reset needs to be clicked twice to reset if resetting from the component that called the trigger

After

https://codesandbox.io/s/examples-query-react-basic-forked-i9vlk?file=/src/App.tsx

reset correctly resets immediately when resetting from the component that called the trigger

@Shrugsy Shrugsy added this to the 1.7 milestone Oct 16, 2021
Comment on lines 828 to 834
if (promise) {
setPromise(undefined)
} else if (fixedCacheKey) {
}
if (fixedCacheKey) {
dispatch(
api.internalActions.removeMutationResult({
requestId,
Copy link
Member

Choose a reason for hiding this comment

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

Hmm. Do we want to wrap the whole thing in a batch now? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In it's current state without batch, calling reset() only seems to result in one render for each component using the hook, including the one that called the original trigger. Is there some other benefit that batch will provide?

Regardless of the above, I'm not against batching it anyway as a "might help, and can't hurt" scenario

Copy link
Member

Choose a reason for hiding this comment

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

It depends how you call it. If you call it in an effect or click handler, React will automatically batch and only rerender once. If you do it in something async, for example after a timeout, it would lead to two renders

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah, for some reason I assumed click handlers were exempt from the default batching. It's definitely worth batching this, I'll change that

@phryneas
Copy link
Member

LGTM :)

@phryneas phryneas merged commit 98b9d9b into reduxjs:v1.7.0-integration Oct 16, 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.

2 participants