Nearby lessons

41 of 44

🛠️ Redux Toolkit in ReactJS

📌 What is Redux Toolkit?

What is Redux Toolkit? Why should we use it?

Redux Toolkit is a set of tools that simplifies Redux development. It is the standard way recommended by the Redux team to write Redux logic in modern applications.

It includes utilities for creating and managing stores, and for writing actions and reducers with less boilerplate. You can say Redux Toolkit is an upgraded and simplified version of Redux.

📌 Why Redux Toolkit?

  • Configuring a Redux store used to be too complicated
  • Needed too many extra packages to make Redux useful
  • Too much boilerplate code with traditional Redux
  • Redux Toolkit hides the difficult parts and improves developer experience

⚙️ Data Flow of Redux Toolkit

Redux Toolkit manages the data flow similar to Redux but in a simpler way:

  1. State is stored in a central store
  2. Components use useSelector to read state
  3. Actions are dispatched using useDispatch
  4. Reducers update the state inside slices
React Playground
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { increment, decrement } from './reduxToolkit/counterSlice';
import 'bootstrap/dist/css/bootstrap.min.css';

const App = () => {
  const myState = useSelector((state) => state.counter.count);
  const dispatch = useDispatch();
  
  return (
    <div className="mt-5">
      <h1>Welcome To React Redux</h1>
      <input type="text" value={myState} className="form-control w-50 mb-3" readOnly />
      <button className="btn btn-primary me-2" onClick={() => dispatch(increment())}>Add</button>
      <button className="btn btn-danger" onClick={() => dispatch(decrement())}>Minus</button>
    </div>
  );
};

export default App;

🪢 Key Features of Redux Toolkit

  • Includes `configureStore` for easy store setup
  • Includes `createSlice` for actions and reducers
  • Uses Immer library internally → lets you write immutable logic easily
  • Reduces boilerplate → fewer files, less code

🧠 Test Your Knowledge

4 Questions

Progress: 0 / 4
Keep Going!React - React.memo