Apollo hooks useQuery and useMutation under the same component [closed]
Asked Answered
C

0

6

I just recently started to learn GraphQL and React-Apollo and I'm wondering what's the recommended approach of using useQuery and useMutation hooks if I need them inside the same component. Since both hooks return {data, loading, error} object the names are conflicting. Should I just assign unique names to the properties while destructuring or is it better to move the logic to separate container components?

Consecrate answered 25/6, 2020 at 8:31 Comment(3)
IMHO it depends on the programming style and the specific situation. There is no definite answer hereCastellan
THis is quite opinion based but here are some things to consider: 1. You seldomly use the data property of the mutation 2. often, your applciation works with the return value of the mutation function (then/catch) 3. you don't have to destructure, xyzMutation.error is fine. I think the really nice thing about hooks is that it is easy and convinient to use multiple at once (thin about useState).Reformation
For anyone who is wondering about the programmatic implications on this question: it is totally valid to use multiple queries/mutations at the same time. To handle the variable naming you can just rename them, like const { data: userData } = useQuery()Gigigigli

© 2022 - 2024 — McMap. All rights reserved.