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?
Asked
Active
Viewed 1,223 times
4

Oleksandr Fomin
- 2,005
- 5
- 25
- 47
-
IMHO it depends on the programming style and the specific situation. There is no definite answer here – Nikita Madeev Jun 25 '20 at 08:34
-
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`). – Herku Jun 25 '20 at 12:14
-
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()` – choise Dec 09 '22 at 11:02