Last active
February 15, 2019 07:58
-
-
Save danielkcz/a264b052ccb3c0d5c098bb3c4bbb7c7e to your computer and use it in GitHub Desktop.
Mutations with Hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// HOC approach was first and some still prefer it for whatever reasons | |
// Along with recompose you can do some real nasty tricks | |
const MutateMe = graphql(MutationDocument, { name: 'execute' })(({ execute }) => { | |
// In comparison it's not a terrible way to execute a mutation | |
// But naming the mutation with string 😆 | |
return <button onClick={() => execute({ variables: { secretName: 'Me' }})} /> | |
}) | |
// Mutation component is fine for simple cases, but gets ugly otherwise | |
const MutateMe = () => { | |
return ( | |
// It's nice you can name the mutation function however you want... | |
<Mutation mutation={MutationDocument}>{execute => | |
<button onClick={() => execute({ variables: { secretName: 'Me' }})} /> | |
}</Mutation> | |
} | |
// And glorified Hooks, I can't help it but it's clear winner in my eyes | |
const MutateMe = () => { | |
// Best of two worlds, you can easily have multiple mutations | |
// and name them however you want (without awkward strings) 👌 | |
const execute = useMutation(MutationDocument) | |
return <button onClick={() => execute({ variables: { secretName: 'Me' }})} /> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment