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

Avoid un-necessary useMutation re-renders #5770

Merged
merged 1 commit into from Jan 9, 2020

Conversation

hwillson
Copy link
Member

@hwillson hwillson commented Jan 9, 2020

apollographql/react-apollo#3417 adjusted useMutation to make sure the current ApolloClient instance is available in the result returned when useMutation is first called. Unfortunately, the changes in that PR are unintentionally modifying the useState based result object directly, instead of using setResult. This is ultimately leading to inconsistencies around the client instance sometimes being included in the result and stored as the previousResult, and other times not being included. This can then lead to infinite loop / too many render problems caused by the !equal(this.previousResult, result) check in updateResult always passing, and the state continuously being updated by repeated calls to setResult.

This commit adjusts the returned useMutation result to be a copy of the original useState based result, which then includes the client instance. This ensures the useState controlled result object is not mutated outside of calling setResult, and avoids the infinite loop / too many render issue.

apollographql/react-apollo#3417 adjusted `useMutation` to make sure
the current `ApolloClient` instance is available in the result
returned when `useMutation` is first called. Unfortunately, the changes
in that PR are unintentionally modifying the `useState` based `result`
object directly, instead of using `setResult`. This is ultimately
leading to inconsistencies around the `client` instance sometimes
being included in the result and stored as the `previousResult`, and
other times not being included. This can then lead to infinite
loop / too many render problems caused by the
`!equal(this.previousResult, result)` check in `updateResult` always
passing, and the state continuously being updated by repeated calls
to `setResult`.

This commit adjusts the returned `useMutation` result to be a copy
of the original `useState` based `result`, which then includes the
`client` instance. This ensures the `useState` controlled `result`
object is not mutated outside of calling `setResult`, and avoids
the infinite loop / too many render issue.
Copy link
Member

@benjamn benjamn left a comment

Choose a reason for hiding this comment

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

Always a fan of non-destructive updates!

@hwillson hwillson merged commit 20c4b70 into master Jan 9, 2020
@hwillson hwillson deleted the use-mutation-rerender-fix branch January 9, 2020 20:05
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants