Putting it all into practice
At this point, you should be able to start handling some use cases for data fetching using the useQuery
hook.
In this section, we will look at a file with three components called ComponentA
, ComponentB
, and ComponentC
that are doing some data fetching. We will use this file to review the concepts we have learned about and see if we fully understood how useQuery
works.
Let’s start with the beginning of the file:
import { useQuery, useQueryClient } from "@tanstack/react-query"; const fetchData = async ({ queryKey }) => { const { apiName } = queryKey[0]; const response = await fetch( `https://danieljcafonso.builtwithdark.com/${apiName}` ); if (!response.ok) throw new Error("Something failed in your request"); return response.json(); }; const apiA = "react-query-api"; const apiB = "react-query-api...