Complete redux toolkit (Part -

WBOY
Release: 2024-09-10 22:35:08
Original
742 people have browsed it

Complete redux toolkit (Part -

Part 3: Introduction to RTK Query

In this part we will cover RTK query

1. What is RTK Query?

While Redux Toolkit provides powerful tools to manage state and asynchronous logic, it still requires significant boilerplate code to handle data fetching and caching.RTK Query, introduced in Redux Toolkit v1.6, aims to solve this problem by providing a set of powerful tools for efficient data fetching and caching with minimal setup.

Key features of RTK Query:

  • Data Fetching and Caching:Automatically handles caching, invalidation, and refetching.
  • Optimistic Updates and Realtime Synchronization:Easily manage optimistic updates and real-time data synchronization.
  • Declarative and Simple API:Intuitive API design with minimal boilerplate code.
  • Integrated with Redux Toolkit:Built on top of Redux Toolkit, allowing seamless integration.

2. Setting Up RTK Query

To get started with RTK Query, we need to define an API service that specifies how to fetch data and what endpoints are available. Let’s create an example using a simple posts API.

Step 1: Define an API Service

Create a new file named postsApi.js in the features/posts directory. This file will define the API endpoints for fetching and mutating posts.

// src/features/posts/postsApi.js import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'; // Define an API service using RTK Query export const postsApi = createApi({ reducerPath: 'postsApi', baseQuery: fetchBaseQuery({ baseUrl: 'https://jsonplaceholder.typicode.com/' }), endpoints: (builder) => ({ fetchPosts: builder.query({ query: () => 'posts', }), addPost: builder.mutation({ query: (newPost) => ({ url: 'posts', method: 'POST', body: newPost, }), }), }), }); // Export hooks for usage in functional components export const { useFetchPostsQuery, useAddPostMutation } = postsApi;
Copy after login

Explanation:

  • createApi: This function is used to define an API service. It generates an API slice, automatically managing the store, reducers, and actions for you.
  • baseQuery: A function that defines the base URL for your API. fetchBaseQuery is a lightweight wrapper around the standard fetch API.
  • endpoints: A function that defines the endpoints for the API. We define two endpoints here: fetchPosts for querying data and addPost for creating a new post.

Step 2: Integrate API Service into the Redux Store

Add the postsApi reducer to the store and configure middleware to enable caching and invalidation.

Update store.js to integrate postsApi:

// src/app/store.js import { configureStore } from '@reduxjs/toolkit'; import { postsApi } from '../features/posts/postsApi'; const store = configureStore({ reducer: { // Add the generated reducer as a specific top-level slice [postsApi.reducerPath]: postsApi.reducer, }, // Adding the api middleware enables caching, invalidation, polling, and other features of RTK Query middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(postsApi.middleware), }); export default store;
Copy after login

3. Using RTK Query in Components

RTK Query generates custom hooks based on the endpoints defined in the API service. These hooks are used to perform data fetching, mutations, and manage caching automatically.

Step 1: Fetching Data with useFetchPostsQuery

Create a PostsList.js component to fetch and display the list of posts.

// src/features/posts/PostsList.js import React from 'react'; import { useFetchPostsQuery } from './postsApi'; const PostsList = () => { const { data: posts, error, isLoading } = useFetchPostsQuery(); if (isLoading) return 

Loading...

; if (error) return

An error occurred: {error.message}

; return (

Posts

    {posts.map((post) => (
  • {post.title}
  • ))}
); }; export default PostsList;
Copy after login

Explanation:

  • useFetchPostsQuery: A custom hook generated by RTK Query for the fetchPosts endpoint. It returns an object containing the fetched data (data), loading state (isLoading), and error state (error).
  • The component conditionally renders loading, error, or data states based on the hook output.

Step 2: Adding Data with useAddPostMutation

Create a AddPostForm.js component to add new posts using the addPost mutation.

// src/features/posts/AddPostForm.js import React, { useState } from 'react'; import { useAddPostMutation } from './postsApi'; const AddPostForm = () => { const [addPost, { isLoading }] = useAddPostMutation(); const [title, setTitle] = useState(''); const [content, setContent] = useState(''); const handleSubmit = async (e) => { e.preventDefault(); if (title && content) { await addPost({ title, body: content }).unwrap(); setTitle(''); setContent(''); } }; return ( 

Add a New Post

setTitle(e.target.value)} placeholder="Post Title" />