Theo's Typesafe CultTTC
Theo's Typesafe Cult13mo ago
9 replies
BadBoy

useQuery or useMutation?

What's the best way to handle side effects with React Query when making API calls in a chat application?

I'm building a chat app where each API response needs to update a conversation array in state. Currently using
useQuery
with side effects in the
queryFn
, but I know this isn't ideal. Here's my current code:

const getAnswer = useQuery({
    queryKey: ["getAnswer", lastConversation?.content],
    queryFn: async () => {
      // API call + state updates here
    },
    enabled: currentStage === STAGES.FINAL_ANSWER,
});


Is useMutation a better choice here? Or should I handle state updates in a separate useEffect?
Was this page helpful?