Understand Redux Toolkit

Deepak Mankotia
4 min readApr 6, 2023

--

Redux Toolkit is an official package from the Redux team that simplifies the process of writing Redux code. It provides a set of opinionated tools and best practices for writing Redux code, with the goal of making it easier and faster to build Redux-based applications.

Redux Toolkit includes several features, such as:

  1. createSlice: a function that generates Redux slice reducers with automatically generated action creators. This simplifies the process of defining and managing Redux state by reducing the amount of boilerplate code you need to write.
  2. configureStore: a function that sets up a Redux store with default middleware, including immutability checks, thunk middleware, and serializable state middleware.
  3. createAsyncThunk: a function that generates action creators for handling asynchronous operations, such as fetching data from an API. It automatically generates actions for pending, fulfilled, and rejected states, simplifying the process of handling asynchronous actions in your Redux store.
  4. createEntityAdapter: a function that generates a set of pre-built reducer functions and selectors for managing normalized data in your Redux store.

By using Redux Toolkit, you can simplify the process of writing Redux code, reduce the amount of boilerplate code you need to write, and speed up the development process. It’s particularly useful for small to medium-sized applications where you don’t need the full power of plain Redux, but still want to use a predictable state container.

How Redux toolkit is useful?
Simplifies code: Redux Toolkit reduces the amount of boilerplate code required to write Redux by providing utility functions like createSlice and createAsyncThunk that abstract away much of the complexity of Redux. This makes the code more concise and easier to read and understand.

  1. Improves performance: Redux Toolkit includes optimized, pre-built reducers and selectors that can improve the performance of your application by reducing the number of unnecessary re-renders.
  2. Enforces best practices: Redux Toolkit includes built-in features that encourage developers to follow best practices when writing Redux code, such as using immutable data structures and avoiding direct mutation of the state.
  3. Reduces errors: By reducing the amount of code that needs to be written and providing pre-built functionality, Redux Toolkit reduces the likelihood of errors in your Redux code.
  4. Faster development: With Redux Toolkit, you can build a fully functional Redux application with less code, less complexity, and fewer potential bugs. This can help to speed up the development process and make it easier to build and maintain large-scale applications.

Overall, Redux Toolkit is a powerful tool that can help to simplify and streamline the process of building Redux applications, making it easier and faster to write clean, maintainable code.

How to implement?
A small example implementation of Redux Toolkit in a React application.

First, you need to install the @reduxjs/toolkit package as a dependency:

npm install @reduxjs/toolkit

Then, you can create a Redux store using configureStore function from @reduxjs/toolkit:

import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './reducers/counter';

export const store = configureStore({
reducer: {
counter: counterReducer,
},
});

In the code above, we create a Redux store using configureStore function and pass a reducer object containing a counter slice that is implemented in ./reducers/counter file.

Here’s an example implementation of a counter slice using the createSlice function from @reduxjs/toolkit:

import { createSlice } from '@reduxjs/toolkit';

const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment(state) {
state.value++;
},
decrement(state) {
state.value--;
},
},
});

export const { increment, decrement } = counterSlice.actions;

export default counterSlice.reducer;

In the code above, we create a counter slice using createSlice function, which generates a slice reducer with automatically generated action creators. We define two actions, increment and decrement, which are automatically generated by createSlice. The slice reducer function updates the state by modifying a mutable state object using immer.js library, which allows us to write code that looks like it's mutating the state, but actually creates a new copy of the state with the changes applied.

Finally, we can use the useSelector and useDispatch hooks from react-redux to access the store state and dispatch actions:

import { useSelector, useDispatch } from 'react-redux';
import { increment, decrement } from './reducers/counter';

function Counter() {
const count = useSelector((state) => state.counter.value);
const dispatch = useDispatch();

return (
<div>
<h1>Count: {count}</h1>
<button onClick={() => dispatch(increment())}>Increment</button>
<button onClick={() => dispatch(decrement())}>Decrement</button>
</div>
);
}

export default Counter;

In the code above, we use useSelector to access the value field of the counter slice state, and useDispatch to get a reference to the store dispatch function. We then use these hooks to update the state by dispatching increment or decrement actions in response to button clicks.

That’s a basic example of how to use Redux Toolkit in a React application. With Redux Toolkit, you can write clean, concise, and maintainable Redux code with fewer lines of code and less boilerplate.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Deepak Mankotia
Deepak Mankotia

Written by Deepak Mankotia

Full Stack Web Developer | Open Source Enthusiast

No responses yet

Write a response