Using TanStack Query to request resources and make changes
In the previous section, we used an Effect Hook to trigger the request, and a Reducer Hook to update the state, using the result from the request. Instead of manually implementing requests like this, we can instead use the TanStack Query library. This library not only allows us to easily fetch resources, but also caches the result for us and provides ways to invalidate the state. Invalidation allows us to, for example, re-fetch posts from the server after creating a new post, instead of having to manually dispatch an action.
Setting up the library
Before we can get started using it, we need to install and set up the library. TanStack Query is a library used to manage state of server data. It consists of 3 parts:
- A Query Client, which manages the cache and invalidation.
- A Query Client Provider, which wraps your application to provide the query client to all components.
- A collection of hooks, such...