Redux vs Redux Toolkit: Choosing the Right State Management Solution

Redux vs Redux Toolkit: Choosing the Right State Management Solution

Managing state in complex JavaScript applications can be challenging. Thankfully, Redux has emerged as a popular state management library, providing a predictable way to handle state changes. However, Redux itself can be verbose and requires writing a lot of boilerplate code. This is where Redux Toolkit comes in. In this article, we will explore the differences between Redux and Redux Toolkit and discuss when to use each solution.


Understanding Redux

Redux is a predictable state container for JavaScript apps. It follows the Flux architecture and provides a centralized store to manage application state. Redux uses actions and reducers to handle state changes. Actions are plain JavaScript objects that describe the type of change, while reducers specify how the state should be updated based on those actions.


Example 1: Counter using Redux

Let's take a look at a simple Redux counter implementation:

// Action Type
const INCREMENT = 'INCREMENT';
const DECREMENT = 'DECREMENT';

// Actions
const increment = () => ({ type: INCREMENT });
const decrement = () => ({ type: DECREMENT });

// Reducer
const counterReducer = (state = 0, action) => {
  switch (action.type) {
    case INCREMENT:
      return state + 1;
    case DECREMENT:
      return state - 1;
    default:
      return state;
  }
};

// Store
const { createStore } = Redux;
const store = createStore(counterReducer);

// Dispatching Actions
store.dispatch(increment());
console.log(store.getState()); // Output: 1

store.dispatch(decrement());
console.log(store.getState()); // Output: 0s        


Example 2: Todo List using Redux

Let's consider a Todo List implementation using Redux:

// Action Type
const ADD_TODO = 'ADD_TODO';
const TOGGLE_TODO = 'TOGGLE_TODO';

// Actions
const addTodo = (text) => ({ type: ADD_TODO, payload: text });
const toggleTodo = (id) => ({ type: TOGGLE_TODO, payload: id });

// Reducer
const todoReducer = (state = [], action) => {
  switch (action.type) {
    case ADD_TODO:
      return [
        ...state,
        {
          id: Date.now(),
          text: action.payload,
          completed: false,
        },
      ];
    case TOGGLE_TODO:
      return state.map((todo) =>
        todo.id === action.payload
          ? { ...todo, completed: !todo.completed }
          : todo
      );
    default:
      return state;
  }
};

// Store
const { createStore } = Redux;
const store = createStore(todoReducer);

// Dispatching Actions
store.dispatch(addTodo('Buy groceries'));
store.dispatch(addTodo('Clean the house'));
store.dispatch(toggleTodo(1));
console.log(store.getState()); // Output: [{ id: 1, text: 'Buy groceries', completed: true }, { id: 2, text: 'Clean the house', completed: false }];        


Introducing Redux Toolkit

While Redux is powerful, it requires writing a lot of boilerplate code to set up the store, define actions, and write reducers. Redux Toolkit is an opinionated, batteries-included package that simplifies the Redux workflow. It provides utilities that streamline common Redux tasks, such as creating actions and reducers.


Example 1: Counter using Redux Toolkit

Now, let's rewrite the previous counter example using Redux Toolkit:

import { createSlice, configureStore } from '@reduxjs/toolkit'

// Slice
const counterSlice = createSlice({
  name: 'counter',
  initialState: 0,
  reducers: {
    increment: (state) => state + 1,
    decrement: (state) => state - 1,
  },
});

// Store
const store = configureStore({ reducer: counterSlice.reducer });

// Dispatching Actions
store.dispatch(counterSlice.actions.increment());
console.log(store.getState()); // Output: 1

store.dispatch(counterSlice.actions.decrement());
console.log(store.getState()); // Output: 0;        


Example 2: Todo List using Redux Toolkit

Let's consider a Todo List implementation using Redux Toolkit:

import { createSlice, configureStore } from '@reduxjs/toolkit'

// Slice
const todoSlice = createSlice({
  name: 'todos',
  initialState: [],
  reducers: {
    addTodo: (state, action) => {
      const { id, text } = action.payload;
      state.push({ id, text, completed: false });
    },
    toggleTodo: (state, action) => {
      const todo = state.find((todo) => todo.id === action.payload);
      if (todo) {
        todo.completed = !todo.completed;
      }
    },
  },
});

// Store
const store = configureStore({ reducer: todoSlice.reducer });

// Dispatching Actions
store.dispatch(todoSlice.actions.addTodo({ id: 1, text: 'Buy groceries' }));
store.dispatch(todoSlice.actions.addTodo({ id: 2, text: 'Clean the house' }));
store.dispatch(todoSlice.actions.toggleTodo(1));
console.log(store.getState()); // Output: [{ id: 1, text: 'Buy groceries', completed: true }, { id: 2, text: 'Clean the house', completed: false }];        

Advantages of Redux Toolkit

Redux Toolkit offers several advantages over plain Redux:

  • Reduced Boilerplate: Redux Toolkit's createSlice function automatically generates action types and action creators, reducing the amount of code you need to write.
  • Immutability Handling: Redux Toolkit uses the Immer library under the hood, making it easier to write immutable updates to the state.
  • DevTools Integration: Redux Toolkit seamlessly integrates with the Redux DevTools Extension, providing a better debugging experience.

When to Use Redux

While Redux Toolkit simplifies the Redux development process, there are still cases where using plain Redux might be preferable:

  • Advanced Use Cases: If you have complex state management needs, such as middleware, custom enhancers, or manual control over store creation, plain Redux gives you more flexibility.
  • Existing Redux Codebase: If you're already using Redux in your project and have an established codebase, migrating to Redux Toolkit might require more effort and may not be necessary unless you encounter specific pain points.


Conclusion

Redux Toolkit provides a simplified and opinionated approach to state management compared to plain Redux. It reduces boilerplate code and offers additional features such as immutability handling and DevTools integration. However, if you have advanced state management requirements or an existing Redux codebase, plain Redux may still be a suitable choice. Consider your project's needs and complexity when deciding between Redux and Redux Toolkit, ensuring that you choose the right state management solution for your application.

Shashikanth H.R

Principal Engineer @ HARMAN India | React | Javascript | Node JS

6 个月

awesome explanation.

赞
回复
Soumya Ranjan Sahu

Android Tech Lead at Fiserv India l Android 11+ YOE | Kotlin | Java || DS | Ruby | React Native | MVVM | MVI | VIPER |Compose | CICD | HIlt | Redux | Flow | Work Manager | ROOM | Co-Routine | Jenkins | Jetpack | Fastlane

11 个月

Superb explanation, Thanks!

Ashwin Umbarkar

FrontEnd Developer | React JS | Javascript | Next JS | Node JS | Express JS | Mongo DB | MERN stack | TypeScript | AWS (ECR to ECS)

1 å¹´

well explained

Adarsh C

Mern Stack Developer | React | JavaScript | Nodejs | Express | MongoDB | Next.js | TypeScript | PostgreSQL | Firebase | Github | Tailwind | Azure | Aws | CICD | Socket.io| Clean | MVC | Repository pattern | Docker

1 å¹´

the way of explaining is awesome.

Saisivaji B

UI Developer | React.JS | Angular | Next.JS | Node.JS | Typescript | Full Stack Development | Frontend Development

1 å¹´

Nice explanation

要查看或添加评论,请登录

Adrian Birta的更多文章

  • What is destructuring in JavaScript useful for?

    What is destructuring in JavaScript useful for?

    If you're diving deeper into JavaScript, you've probably come across the term "destructuring." It sounds fancy, almost…

  • Threads and Processes: A Tale of JavaScript's Inner Workings

    Threads and Processes: A Tale of JavaScript's Inner Workings

    Imagine your computer as a bustling city. In this city, a process is like an entire factory.

  • CSS Techniques: Elevate Your Web Design Skills

    CSS Techniques: Elevate Your Web Design Skills

    As web development continues to evolve, mastering CSS becomes increasingly vital for creating sophisticated and…

  • Introducing the ECMAScript Safe Assignment Operator: Simplifying Error Handling in JavaScript

    Introducing the ECMAScript Safe Assignment Operator: Simplifying Error Handling in JavaScript

    Warning - This proposal is actively under development! JavaScript is evolving, and one of the most exciting proposals…

    2 条评论
  • Enhancing E-Commerce Experiences with Interactive Hotspots

    Enhancing E-Commerce Experiences with Interactive Hotspots

    Recently, I had the opportunity to work on a project that integrates hotspots seamlessly into an e-commerce platform…

  • SVG Unit Display Tool - Part 2

    SVG Unit Display Tool - Part 2

    I’m pleased to share some recent updates to the SVG Unit Display Tool, a web application aimed at simplifying the…

  • SVG Unit Display Tool - Part 1

    SVG Unit Display Tool - Part 1

    In the world of real estate, architecture, and interior design, visualizing space is crucial. Interactive floor plans…

  • TypeScript Types Cheat Sheet

    TypeScript Types Cheat Sheet

    TypeScript's type system empowers developers to write more robust and maintainable code by enforcing static types. This…

  • TypeScript: Differences Between Types and Interfaces

    TypeScript: Differences Between Types and Interfaces

    In the world of TypeScript, understanding the distinctions between types and interfaces is fundamental. While they…

  • CSS Modules vs Styled Components

    CSS Modules vs Styled Components

    Styling in React applications is an art, and two prominent techniques, CSS Modules and Styled Components, vie for the…

社区洞察

其他会员也浏览了