Using TanStack Query
TanStack Query, more commonly known as React 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.
To get started, let’s install the library as a dependency for our project:
npm install @tanstack/react-query
Next, we need to configure the library by adding the QueryClientProvider
:
const queryClient = new QueryClient();
ReactDOM.createRoot(document.getElementById("root")!).render(
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
);
After this setup, we can start working on the app. One of the unique features of this library is that it is agnostic to the tool you use for...