worth a read
React Query as a State Manager
Data Fetching
//CUSTOM HOOK
type Todo = {
id: number
state: State
}
type Todos = ReadonlyArray<Todo>
const fetchTodos = async (id: number): Promise<Todos> => {
const response = await axios.get(`todos/${id}`)
return response.data
}
export const useTodos = () =>
useQuery({ queryKey: ['todos'], queryFn: fetchTodos })
import React from 'react'
const ExampleComponent = () => {
const { data } = useTodos()
return (
<div>
<h1>{data?.title}</h1>
</div>
)
}
Some Key points to keep in mind
https://twitter.com/asidorenko_/status/1623751929863016450?s=20
Error Handling
I prefer using a combination of react-query + react error boundary to catch all the errors in our application, async as well as runtime