14/05/2023
Introduction to RTK Query ❗
1️⃣ Introduction
RTK Query is a library for managing API queries in Redux applications.
It provides a simple and powerful way to manage request status, caching and re-ex*****on of requests. RTK Query also integrates with standard Redux tools such as Redux Toolkit and Redux DevTools.
2️⃣ Working Principle
The way RTK Query works is that it creates a cache to store the results of API queries. When a React component requests data, RTK Query checks if the data is in the cache and returns it if it is present. If there is no data in the cache, RTK Query makes a query to the API and stores the result in the cache.
3️⃣ createApi()
This is the main RTK Query function that lets you create an API with specified configuration options. It takes a configuration options object that contains information about API endpoints, query options, caching options and other parameters.
4️⃣ useQuery()
This is a React hook that makes it easy to run API queries from React components.
It takes the API endpoint name and query options, and returns an object with query data, query state, and a method to re-execute the query.
5️⃣ useMutation()
This is a function that allows you to send data to the server and process the results of the query ex*****on. When you call useMutation, RTK Query automatically generates an action function that executes the request to the server and processes the results. It returns an object that contains information about the status of the request, such as load, success, or error.
6️⃣ Redux Toolkit
RTK Query integrates with Redux Toolkit, making it even more user-friendly.
The library includes many Redux actions and selectors, making it easy to manage application state and update data in real time.