codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚛️

React.js

28 / 61 topics
28Introduction to Redux29Creating a Redux Store30Actions and Reducers in Redux31Middleware in Redux32Integrating React with Redux33Handling Async Actions with Redux Thunk34Introduction to Redux Saga35Using Saga Effects in Redux Saga
Tutorials/React.js/Introduction to Redux
⚛️React.js

Introduction to Redux

Updated 2026-04-20
3 min read

Introduction to Redux

In this section, we will explore Redux, a predictable state container for JavaScript applications. Redux is widely used with React.js to manage the application's global state in a centralized manner. This tutorial will cover the basics of Redux, including its core concepts, installation, and integration with React.js.

What is Redux?

Redux is a library that helps you write applications that behave consistently across client, server, and native environments, and are easy to test. It uses a single immutable state tree, actions to describe state changes, and pure functions called reducers to update the state based on those actions.

Core Concepts of Redux

  1. State: The global state of your application is stored in a single object tree within a single store.
  2. Actions: Actions are payloads of information that send data from your application to your Redux store.
  3. Reducers: Reducers specify how the application's state changes in response to actions sent to the store.

Why Use Redux?

  • Predictable State Management: Redux provides a predictable way to manage state, making it easier to debug and test.
  • Centralized State: All the state of your application is stored in one place, which simplifies debugging and state management.
  • Developer Tools: Redux comes with powerful developer tools that allow you to inspect the state changes over time.

Installation

To use Redux with React.js, you need to install a few packages:

npm install redux react-redux @reduxjs/toolkit

Setting Up Redux in a React Application

Let's walk through setting up Redux in a simple React application.

1. Create the Redux Store

First, create a store using configureStore from @reduxjs/toolkit.

// src/store.js
import { configureStore } from '@reduxjs/toolkit';

const initialState = {
  count: 0,
};

function counterReducer(state = initialState, action) {
  switch (action.type) {
    case 'increment':
      return { ...state, count: state.count + 1 };
    case 'decrement':
      return { ...state, count: state.count - 1 };
    default:
      return state;
  }
}

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

export default store;

2. Provide the Redux Store to Your React Application

Wrap your application in a Provider component from react-redux, passing the store as a prop.

// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import App from './App';
import store from './store';

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);

3. Connect React Components to the Redux Store

Use the useSelector and useDispatch hooks from react-redux to connect your components to the Redux store.

// src/App.js
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';

function App() {
  const count = useSelector((state) => state.count);
  const dispatch = useDispatch();

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

export default App;

Best Practices

  1. Normalize State Shape: Keep your state flat and normalize it to avoid deeply nested structures.
  2. Use Redux Toolkit: It simplifies the process of writing Redux logic by providing utilities like createSlice and configureStore.
  3. Immutable Updates: Always return a new state object in reducers instead of mutating the existing one.

Advanced Concepts

Middleware

Middleware allows you to add custom functionality between dispatching an action and the moment it reaches the reducer. For example, Redux Thunk is used for handling asynchronous operations.

// src/store.js
import { configureStore } from '@reduxjs/toolkit';
import thunk from 'redux-thunk';

const store = configureStore({
  reducer: counterReducer,
  middleware: [thunk],
});

export default store;

Asynchronous Actions

Use Redux Thunk to handle asynchronous operations like API calls.

// src/store.js
function fetchUser(userId) {
  return async (dispatch, getState) => {
    const response = await fetch(`https://api.example.com/users/${userId}`);
    const user = await response.json();
    dispatch({ type: 'user/fetched', payload: user });
  };
}

Conclusion

Redux is a powerful tool for managing state in large React applications. By centralizing your application's state, you can make it easier to reason about and maintain. In this tutorial, we covered the basics of Redux, including setting up a store, connecting components, and using middleware for asynchronous operations.

In the next section, we will dive deeper into advanced Redux concepts and best practices to help you build robust and scalable applications.


PreviousProtected Routes in React RouterNext Creating a Redux Store

Recommended Gear

Protected Routes in React RouterCreating a Redux Store