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

29 / 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/Creating a Redux Store
⚛️React.js

Creating a Redux Store

Updated 2026-04-20
4 min read

Introduction

In this tutorial, we will delve into the process of creating a Redux store in a React.js application. Redux is a predictable state container for JavaScript applications, and it's particularly useful for managing global state across multiple components in large-scale applications. By the end of this guide, you'll have a solid understanding of how to set up and configure a Redux store, along with best practices for integrating it into your React project.

Prerequisites

Before we begin, ensure that you have the following:

  • A basic understanding of React.js.
  • Node.js installed on your machine.
  • A text editor or an IDE (Integrated Development Environment) like VSCode.
  • A React application set up. If not, you can create one using Create React App by running npx create-react-app my-redux-app.

Step 1: Install Redux and React-Redux

First, we need to install the necessary packages for Redux and React integration. Open your terminal, navigate to your React project directory, and run the following commands:

npm install redux react-redux

This will add Redux and React-Redux to your project dependencies.

Step 2: Create a Redux Store

Now that we have Redux installed, let's create a store. A Redux store holds the entire state tree of your application. The only way to change the state inside it is to dispatch an action on it.

Creating the Store File

Create a new file named store.js in the src directory of your React project. This file will contain the configuration for our Redux store.

// src/store.js
import { createStore } from 'redux';

// Define the initial state of your application
const initialState = {
  count: 0,
};

// Create a reducer function
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;
  }
}

// Create the Redux store
const store = createStore(counterReducer);

export default store;

Explanation

  • Initial State: We define an initial state object with a count property set to 0.
  • Reducer Function: A reducer is a pure function that takes the current state and an action, and returns a new state. In this example, we have two actions: INCREMENT and DECREMENT, which modify the count property.
  • Creating the Store: We use the createStore function from Redux to create our store, passing in the reducer function.

Step 3: Provide the Redux Store to Your React Application

To make the Redux store available to your entire React application, you need to wrap your root component with the <Provider> component from React-Redux. This allows any nested components to access the Redux store.

Modifying index.js

Open the src/index.js file and modify it as follows:

// 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')
);

Explanation

  • Provider Component: The <Provider> component makes the Redux store available to any nested components that have been wrapped in the connect() function or use the useSelector and useDispatch hooks from React-Redux.

Step 4: Connect Your Components to the Redux Store

Now that your store is set up and provided to your application, you can connect your components to it. Let's create a simple component that displays the count and allows incrementing or decrementing it.

Creating Counter.js

Create a new file named Counter.js in the src directory:

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

const Counter = () => {
  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 Counter;

Explanation

  • useSelector Hook: This hook allows you to extract data from the Redux store state. In this example, we use it to get the count value.
  • useDispatch Hook: This hook returns a reference to the dispatch function from the Redux store, which you can use to dispatch actions.

Using the Counter Component

Finally, use the Counter component in your App.js file:

// src/App.js
import React from 'react';
import Counter from './Counter';

function App() {
  return (
    <div className="App">
      <h1>Redux Counter Example</h1>
      <Counter />
    </div>
  );
}

export default App;

Step 5: Test Your Redux Store

Start your React application by running npm start in your terminal. You should see a counter displayed on the screen with buttons to increment and decrement the count. Each action you take will update the state managed by the Redux store, demonstrating how Redux works in a React application.

Best Practices

  1. Separate Reducers: For larger applications, it's best practice to separate your reducers into different files and combine them using combineReducers from Redux.
  2. Use Action Creators: Instead of dispatching plain action objects directly, consider using action creators. These are functions that return action objects, making your code cleaner and more maintainable.
  3. Immutable Updates: Always ensure that you're updating state immutably in your reducers. Use tools like immer to simplify this process.
  4. Middleware for Asynchronous Actions: For handling asynchronous actions (e.g., API calls), use Redux middleware like redux-thunk or redux-saga.

Conclusion

In this tutorial, we've covered the basics of creating a Redux store in a React.js application. We learned how to set up the store, provide it to our components, and connect those components to the store using hooks. By following these steps and best practices, you'll be well on your way to managing global state effectively in your React applications.

Feel free to explore more advanced features of Redux, such as middleware for handling asynchronous actions, integrating with Redux DevTools for debugging, or using selectors for efficient state access. Happy coding!


PreviousIntroduction to ReduxNext Actions and Reducers in Redux

Recommended Gear

Introduction to ReduxActions and Reducers in Redux