Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Adjust useMutation result tuple to be more inline with React Core #3199

Merged
merged 4 commits into from
Jun 28, 2019

Conversation

hwillson
Copy link
Member

@hwillson hwillson commented Jun 28, 2019

This commit re-arranges the useMutation result tuple such that the mutation result is in the first position, with the mutation function in the second position. The intent of this change is to bring the useMutation Hook more in-line with how the React Core handles its Hook results (e.g. useState, useReducer).

This is a breaking change, but since we're in beta hopefully that's okay. To help lessen the blow a bit this commit includes changes to display a console.warn message describing the API changes. This message will be removed before the final version of React Apollo 3 is published.

Fixes #3189.

hwillson added 2 commits June 28, 2019 14:34
This commit re-arranges the `useMutation` result tuple such that
the mutation result is in the first position, with the mutation
function in the second position. The intent of this changes is to
bring the `useMutation` Hook more in-line with how the React Core
handles its Hook results (e.g. `useState`, `useReducer`).

This is a breaking change, but since we're in beta hopefully that's
okay. To help lessen the blow a bit this commit includes changes to
display a `console.warn` message describing the API changes. This
message will be removd before the final version of React Apollo 3
is published.

Fixes #3189.
@hwillson hwillson requested a review from benjamn June 28, 2019 19:09
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.

Glad we’re doing this during the beta testing period!

packages/hooks/src/useMutation.ts Outdated Show resolved Hide resolved
packages/hooks/README.md Show resolved Hide resolved
@hwillson hwillson merged commit 1a69cd2 into master Jun 28, 2019
@hwillson hwillson deleted the issue-3189 branch June 28, 2019 22:36
@danielkcz
Copy link
Contributor

Just want to point out that docs (README) are kinda mixed, it shows correct signature coming from this change, but the example below has the "old way". That got me confused and had to look into a source to verify it.

@hwillson
Copy link
Member Author

hwillson commented Jul 5, 2019

Great point @FredyC - thanks! We're going to be overhauling all of the README's very shortly.

@danielkcz
Copy link
Contributor

danielkcz commented Jul 5, 2019

Either way, I am a bit surprised by this change, to be honest. The point is, that user might want to execute a mutation and get the result from returned Promise more often than reading it out from scoped result. That means we will have to be skipping tuple variable, eg. const [, mutate] = useMutation(...).

It's not a big deal breaker, but on the point of aligning with React Core, there is never use case like that (eg. calling setState without reading value) so the reasoning might be a little bit off in my opinion.

@pie6k
Copy link

pie6k commented Jul 9, 2019

I'm also not fan of this. Having mutation result as promise data should be enough in all cases - if you need, you can just assign it to the state yourself or even create custom hook. It also means component will re-render after mutation which is not needed in many cases.

@hwillson
Copy link
Member Author

@pie6k Could you elaborate on your comment a bit further? All this is doing is switching the position of the mutation function and the mutation result object in the returned useMutation tuple. I don't quite follow how this will impact rendering?

@danielkcz
Copy link
Contributor

@hwillson He probably means if there wouldn't be any internal state for mutation, it wouldn't need to rerender and you would get a result from the Promise... But then there wouldn't much of a difference from calling client.mutate imo :)

No, I think that the return result has its uses, especially the loading state which is great to express waiting on the operation to finish. It was actually the reason why I made this PR in first place trojanowski/react-apollo-hooks#93

Thanks for reconsidering switching arguments over, it indeed makes more sense in most cases.

@reverie
Copy link

reverie commented Jul 12, 2019

This is an important one to get right.

To rephrase what FredyC said, I think the analogy to "how the React Core handles its Hook result" is misguided. Keeping data types in the same order might be the principle to follow if all else were equal. But the principle of 'optional values should come last' is a stronger convention (like in function arguments), and it's more strongly grounded in practice as well (it avoids [, noise all over the place).

@danielkcz
Copy link
Contributor

@reverie Hold your horses, there are already considering switching tuple values around ... trojanowski/react-apollo-hooks#187 (comment)

@reverie
Copy link

reverie commented Jul 13, 2019

@FredyC When people are considering it is the right time to comment, right?

@danielkcz
Copy link
Contributor

danielkcz commented Jul 13, 2019

I wouldn't be making such a fuss around it really. If anyone is bothered by [, it's trivial to make a custom hook that switches it around. I will be content, either way. I already have some abstractions around it anyway (to handle errors and smooth out api).

@reverie
Copy link

reverie commented Jul 13, 2019

Seems like you are making a fuss about it :)

@hwillson
Copy link
Member Author

Hi all - we're going to switch it back; we just haven't had a chance yet (we just finished a company get together week, so time has been a bit tight). Everything will be back to normal this week, and this (along with other outstanding changes) will be wrapped up. Thanks!

hwillson added a commit that referenced this pull request Jul 17, 2019
While the changes requested in #3189 and implemented in #3199
seemed like a good idea, we no longer think it makes sense for
React Apollo to align its hooks API with React's core hooks API.
In `useMutation`'s case, the mutation function will almost always
be called, whereas the results aren't necessarily used. For this
reason it makes sense to keep the mutation function in the first
position of the result tuple.
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.

useMutation Tuple signature inconsistent with built-in hooks
5 participants