Before we get into the technicality of using RTK Query in place of React query, let's understand the basics and know what actually RTK Query is.
RTK Query is nothing but an advanced tool to fetch data on the client side. It is basically a caching tool and it works similarly to React. The only good thing about RTK Query is that it directly integrates with Redux. When interacting with APIs, developers frequently use Redux with middleware modules like Thunk.
Adding RTK Query to Redux Toolkit is not mandatory, but when you combine both of them in a React project, it brings out the true power of Redux Toolkit. However, it's advisable to use the official Redux+JS template or the Redux+TS template to initialize a new app with React and Redux.
To help you learn, we've created a React App project with all of the code in the src folder. We will provide some best practices to follow when utilizing Redux Toolkit with React in addition to instructing you on how to use RTK queries in React apps.
Install the React-Rtk package:
To get it started, let's start with using the official Redux+JS template or Redux+TS template. It is way easier and faster to create a React app that uses Redux. This also prevents you from making mistakes.
Here are the complete steps to add Redux Toolkit to an old React project:
There are a thousand ways to learn React programming, but if you want to learn it from scratch, then you are on the right platform. Here are detailed instructions for configuring Redux Toolkit and RTK Query with React. You will be able to comprehend the intricacies of the Redux Toolkit.
Let's start with an NPM because most developers are familiar with it. You can also use any other package manager according to availability, as it will not affect the written code.
Launch a New React App
Let's first initialize a new React app if you don't already have one before we begin fetching and installing the necessary dependencies.
Use this command to launch a new TypeScript-enabled React app.
The project will take a few minutes to get initiated.
Install React-Redux and Redux Toolkit
First of all, you have to install Redux Toolkit and React-redux within the project.
As Redux Toolkit is already installed in the project in Typescript format so we only have to install react-Redux using the below command:
React Redux has a dependency on @types/react-redux so the type definition file of the package will be automatically installed with it.
Create Redux Store
Create a redux folder inside the src folder, and within this redux folder, create a store.ts file.
Redux Toolkit is already written in Typescript, so we don’t need to worry about installing its type definition files separately.
There is no need to include configureStore in addition.
Define Root State
Extract RootState and AppDispatch and export it directly from the store.ts file.
RootState and AppDispatch inferred from the store will update as you add state slices, API services, or modify middleware settings.
Insert Redux Store
Since the store is created, it must be provided to all application components.
Import ./redux/store and the <Provider> component from react-redux to the index.tsx file.
Wrap the app component in the provider component, then give the provider the store as a prop.
Define the State Selector
The RootState and AppDispatch types that we defined in the store.ts file can be imported into each component, however, it is advised to create typed versions of the useDispatch and useSelector hooks to be used throughout your entire application.
Dispatch Typed Hooks
Create a new hooks.ts file within the redux folder ./src/redux.
Write the below code to add the snippets.
Create a Redux State Slice
After defining Hooks and State Selector now it’s time to create our first redux state slice.
Create a features directory Inside the redux folder to house all our slices.
Create a new file with the name: src/redux/features/products/authSlice.ts.
Copy and paste the below code in the authSlice.ts file.
Those who have used Redux code in the past must know that it requires mentioning all state updates immutably by making copies of the state.
Redux Toolkit gives us the luxury of directly mutating any state.
Add Reducer of the Redux State Slice
In the next step, we will import the reducer function.
To do this, we will export the authSlice.ts into the store and add it to the reducers object.
By defining the authUser field inside of the reducer object, we can communicate to the store that the authReducer function should be used to manage any updates to that state.
Add RTK Query
RTK Query will help us retrieve API data from the cache. It is designed on top of the Redux Toolkit and uses its APIs for implementation.
It is not required to use RTK Query with Redux Toolkit; however, it is highly recommended to combine both Redux Toolkit and RTK Query in your project if you will be managing both local state and API data. While it is not required to use RTK Query with Redux Toolkit, it is highly recommended to do so.
You are free to make use of other API state management tools such as React Query; however, using Redux Toolkit to get it set up and operational can be a real pain in the rear.
Because of this, the Redux team came to the conclusion that it would be beneficial to develop RTK Query, which has a user interface that is analogous to that of React Query but is more compatible with Redux Toolkit and is less complicated to operate.
Create an API Service
It is necessary to create an API service within the redux directory.
Create an API file named src/redux/api/products/productAPI.ts
An example of a CRUD operation performed with RTK Query is shown below. In this example, I am retrieving all of the products, obtaining a single product, updating a single product, and deleting a single product.
createAPI will take a single configuration object that may have any of the following parameters if it is provided.
Adding the final API Service to the Redux Store
RTK Query will produce what is known as a "slice reducer" from the productAPI, which is intended to be added to the Redux root reducer.
Additionally, a specialised middleware will be generated, and the middleware parameter will need to be updated to include it.
Final thoughts:
RTK Query implementation in a React app is critical for creating efficient and powerful apps. Developers can make applications that work well and are easy to maintain by using the query syntax and providing the necessary data. Even though the process can be difficult, the end result has many benefits and can help developers make apps with more features that can be used for a wide range of tasks. With the right guidance, developers can easily and effectively implement RTK Query in a React app and take their applications to the next level.
No comments:
Post a Comment