Using TanStack Query
TanStack Query is a library that has taken server interaction to a new level. This library allows us to request data and cache it. As a result, we can call the same useQuery Hook a lot of times during one rendering, but only one request will be sent to the server. The library also includes built-in loading and error states, simplifying the handling of request states. Let's see how to use it.
The first step is to configure the library by adding QueryClientProvider to main.tsx:
const queryClient = new QueryClient();
createRoot(document.getElementById("root")!).render(
<StrictMode>
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
</StrictMode>
);
One of the unique features of this library is that it is agnostic to the tool you use for data fetching. You just need to provide a promise function that returns the data. Let's see such a function using the Fetch API:
const userFetcher...