DEV Community

bilal khan
bilal khan

Posted on

Guide to Redux, React-Redux, and Redux Toolkit

Managing state in a growing application can be daunting. Redux, along with React-Redux and Redux Toolkit, offers a robust solution for predictable and scalable state management. In this guide, we’ll explore these tools comprehensively, from their core concepts to practical implementation in React.

Official Documentation

Dive deeper by visiting the official Redux Documentation.


What is Redux?

Redux is a predictable state container for JavaScript applications. It centralizes the state in a single source of truth (store) and ensures predictable state updates through pure functions called reducers.

Key Features

  1. Single Source of Truth: State is stored in one global object.
  2. Predictability: State changes are controlled and follow clear rules.
  3. Debugging: Redux DevTools enables tracing state changes.
  4. Middleware: Enhances Redux, supporting features like async actions.

Core Principles

  1. Single Source of Truth: One store holds the entire app’s state.
  2. State is Read-Only: The only way to update the state is by dispatching actions.
  3. Changes are Made with Pure Functions: Reducers handle state changes predictably.

The Redux Flow

  1. Action: Plain objects describing what to do (e.g., INCREMENT, ADD_TODO).
   const incrementAction = { type: 'INCREMENT', payload: 1 };
Enter fullscreen mode Exit fullscreen mode
  1. Reducer: A pure function defining how state is updated.
   const counterReducer = (state = 0, action) => {
     switch (action.type) {
       case 'INCREMENT': return state + action.payload;
       case 'DECREMENT': return state - action.payload;
       default: return state;
     }
   };
Enter fullscreen mode Exit fullscreen mode
  1. Store: Holds the entire state and provides methods to access and modify it.
   import { createStore } from 'redux';
   const store = createStore(counterReducer);
Enter fullscreen mode Exit fullscreen mode
  1. Dispatch: Sends actions to the store.
   store.dispatch({ type: 'INCREMENT', payload: 1 });
Enter fullscreen mode Exit fullscreen mode
  1. Selectors: Functions to extract and use specific parts of the state.
   const selectCounter = (state) => state.counter;
Enter fullscreen mode Exit fullscreen mode

React-Redux: Connecting Redux with React

React-Redux simplifies using Redux in React applications. It provides hooks and utilities for binding the Redux store to React components.

Key Features

  1. Provider: Wraps your app, giving components access to the Redux store.
  2. Hooks:
    • useSelector: Access state.
    • useDispatch: Dispatch actions.

Setup Steps

  1. Install React-Redux:
   npm install react-redux
Enter fullscreen mode Exit fullscreen mode
  1. Wrap your app with Provider:
   import { Provider } from 'react-redux';
   import store from './store';

   ReactDOM.render(
     <Provider store={store}>
       <App />
     </Provider>,
     document.getElementById('root')
   );
Enter fullscreen mode Exit fullscreen mode
  1. Use Redux in components:
   import { useSelector, useDispatch } from 'react-redux';

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

     return (
       <div>
         <h1>{count}</h1>
         <button onClick={() => dispatch({ type: 'INCREMENT', payload: 1 })}>
           Increment
         </button>
       </div>
     );
   };
Enter fullscreen mode Exit fullscreen mode

Redux Toolkit: Simplifying Redux Development

Redux Toolkit (RTK) is the official, recommended way to use Redux. It abstracts boilerplate and provides utilities for efficient Redux development.

Why Redux Toolkit?

  1. Simplifies setup with preconfigured defaults.
  2. Handles immutability using Immer.
  3. Includes Redux DevTools and middleware.

Setup Steps

  1. Install Redux Toolkit:
   npm install @reduxjs/toolkit
Enter fullscreen mode Exit fullscreen mode
  1. Create a slice:
   import { createSlice } from '@reduxjs/toolkit';

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

   export const { increment, decrement } = counterSlice.actions;
   export default counterSlice.reducer;
Enter fullscreen mode Exit fullscreen mode
  1. Create and configure the store:
   import { configureStore } from '@reduxjs/toolkit';
   import counterReducer from './counterSlice';

   export const store = configureStore({
     reducer: { counter: counterReducer },
   });
Enter fullscreen mode Exit fullscreen mode
  1. Provide the store:
   import { Provider } from 'react-redux';
   import { store } from './store';

   ReactDOM.render(
     <Provider store={store}>
       <App />
     </Provider>,
     document.getElementById('root')
   );
Enter fullscreen mode Exit fullscreen mode
  1. Use Redux state and actions:
   import { useSelector, useDispatch } from 'react-redux';
   import { increment, decrement } from './counterSlice';

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

     return (
       <div>
         <h1>{value}</h1>
         <button onClick={() => dispatch(increment())}>Increment</button>
         <button onClick={() => dispatch(decrement())}>Decrement</button>
       </div>
     );
   };
Enter fullscreen mode Exit fullscreen mode

When to Use Redux?

Redux is a powerful tool but may not be necessary for every project. Use Redux when:

  1. The state logic is complex or shared across components.
  2. You need strict control over state transitions.
  3. Debugging state changes is a priority.

For simpler applications, React’s Context API or local state might suffice.


Conclusion

Redux, React-Redux, and Redux Toolkit together provide a complete solution for state management in React applications. While Redux ensures predictable state changes, Redux Toolkit simplifies the development process, and React-Redux seamlessly integrates Redux into React.

By following this guide, you can efficiently implement state management in your applications. Check out the official Redux Documentation for more insights. Stay tuned for our upcoming posts, where we’ll explore using Redux with backend APIs in a MERN stack project!

Top comments (0)