Introduction to React-Redux
Last Updated :
14 Aug, 2025
React-Redux is a popular state management library that helps manage the application state in React applications. It helps manage an application's state in a centralized way, making it easier to maintain and debug, especially as the app grows in complexity.
It is based on three fundamental principles:
- Single Source of Truth: The entire application state is stored in a single JavaScript object called the store.
- State is Read-Only: The only way to change the state is by dispatching an action that describes the change.
- Changes are Made with Pure Functions: Changes to the state are made using reducers, which are pure functions that take the previous state and an action, and return the new state.
Core Concepts of React-Redux
To effectively use React-Redux, you need to understand some key concepts:
1. Store
The store is a centralized object that holds the application state. It is the only place where the state can be modified.
2. Actions
An action is a plain JavaScript object that describes a change in the application state. It must have a type property and can optionally include a payload.
const incrementAction = {
type: 'INCREMENT',
payload: 1
};
3. Reducers
A reducer is a pure function that specifies how the application’s state changes in response to an action. It takes the current state and an action as arguments and returns a new state.
const counterReducer = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + action.payload;
case 'DECREMENT':
return state - action.payload;
default:
return state;
}
};
4. Dispatch
The dispatch function is used to send an action to the Redux store, triggering the reducer to process the state change.
store.dispatch(incrementAction);
5. Selectors
A selector is a function that retrieves specific pieces of state from the Redux store. It helps to abstract the state retrieval process, making it easier to access state in a more manageable way.
const selectCount = (state) => state.count;
6. Provider
The Provider component makes the Redux store available to all the components in the application. It should wrap the entire application so that any component can access the store.
import { Provider } from 'react-redux';
<Provider store={store}>
<App />
</Provider>;
7. connect()
connect() is a function provided by React-Redux to connect React components to the Redux store. It allows the components to access the state and dispatch actions.
import { connect } from 'react-redux';
const Counter = ({ count, increment }) => (
<div>
<h1>{count}</h1>
<button onClick={increment}>Increment</button>
</div>
);
const mapStateToProps = (state) => ({
count: state.count
});
const mapDispatchToProps = (dispatch) => ({
increment: () => dispatch({ type: 'INCREMENT', payload: 1 })
});
export default connect(mapStateToProps, mapDispatchToProps)(Counter);
React-Redux Working
React-Redux connects React components to the Redux store, ensuring smooth state management across your app. Here’s a simplified breakdown of how it works:
1. Setting Up the Store
The Redux store holds the entire state of your application. It’s created using createStore() and initialized with a reducer to define how the state changes.
const store = createStore(counterReducer);
2. Dispatching Actions
Actions are plain objects that describe changes in state. These actions are dispatched to inform Redux of a state change.
store.dispatch({ type: 'INCREMENT', payload: 1 });
3. Reducers Update the State
A reducer is a function that updates the state based on the action type. It takes the current state and action, then returns a new state.
const counterReducer = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT': return state + action.payload;
default: return state;
}
};
4. Connecting Components with connect()
React-Redux’s connect() function connects React components to the Redux store, allowing components to access state and dispatch actions.
const mapStateToProps = (state) => ({ count: state });
const mapDispatchToProps = (dispatch) => ({
increment: () => dispatch({ type: 'INCREMENT', payload: 1 })
});
5. Using Provider to Make Store Accessible
The Provider component makes the store available to all components in the app.
<Provider store={store}><App /></Provider>
6. Re-Renders and Reactivity
React-Redux ensures that only components dependent on updated state will re-render, optimizing performance.
Steps To Implement React-Redux
Step 1: Setting Up the Project
First, create a new React app using create-react-app:
npx create-react-app react-redux-countercd react-redux-counter
Next, install redux and react-redux:
npm install redux react-redux
Folder Structure
Folder StructureDependencies
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-redux": "^9.1.2",
"react-scripts": "5.0.1",
"redux": "^5.0.1",
"web-vitals": "^2.1.4"
}
Step 2: Define Action Types
You need to define action types, which represent the actions that will update the state.
JavaScript
// src/redux/actionTypes.js
export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
Step 3: Action Creators
Action creators are functions that return action objects.
JavaScript
// src/redux/actions.js
import { INCREMENT, DECREMENT } from "./actionTypes";
export const increment = () => ({
type: INCREMENT,
});
export const decrement = () => ({
type: DECREMENT,
});
Step 4: Reducers
Reducers specify how the application's state changes in response to actions. The reducer function receives the current state and action, and returns the updated state.
JavaScript
// src/redux/reducer.js
import { INCREMENT, DECREMENT } from "./actionTypes";
const initialState = {
count: 0,
};
const 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;
}
};
export default counterReducer;
Step 5: Create the Redux Store
Now, create the Redux store using the createStore function from Redux.
JavaScript
// src/redux/store.js
import { createStore } from 'redux';
import counterReducer from './reducer';
const store = createStore(counterReducer);
export default store;
Step 5: Wrap the App with the Redux Provider
Next, wrap your entire application with the Redux Provider to make the store accessible to all components in the app.
CSS
/* src/index.css */
body {
font-family: Arial, sans-serif;
text-align: center;
}
button {
margin: 5px;
padding: 10px;
font-size: 16px;
}
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import store from './redux/store';
import App from './App';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
app.js
import React from "react";
import Counter from "./components/Counter";
function App() {
return (
<div className="App">
<h1>React-Redux Counter App</h1>
<Counter />
</div>
);
}
export default App;
Counter.js
import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { increment, decrement } from "../redux/actions";
const Counter = () => {
const count = useSelector((state) => state.count);
const dispatch = useDispatch();
return (
<div>
<p>Count: {count}</p>
<button onClick={() => dispatch(increment())}>Increment</button>
<button onClick={() => dispatch(decrement())}>Decrement</button>
</div>
);
};
export default Counter;
To start the application run the following command.
npm start
Output
Similar Reads
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.Basics of Web Development To better understand t
7 min read
HTML Tutorial
CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
7 min read
JS Tutorial
JavaScript TutorialJavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.Client Side: On the client side, JavaScript works
8 min read
JSON TutorialJSON (JavaScript Object Notation) is a widely-used, lightweight data format for representing structured data. Used Extensively : Used in APIs, configuration files, and data exchange between servers and clients.Text-based: JSON is a simple text format, making it lightweight and easy to transmit.Human
5 min read
TypeScript TutorialTypeScript is a superset of JavaScript that adds extra features like static typing, interfaces, enums, and more. Essentially, TypeScript is JavaScript with additional syntax for defining types, making it a powerful tool for building scalable and maintainable applications.Static typing allows you to
8 min read
Vue.js TutorialVue.js is a progressive JavaScript framework for building user interfaces. It stands out for its simplicity, seamless integration with other libraries, and reactive data binding.Built on JavaScript for flexible and component-based development.Supports declarative rendering, reactivity, and two-way d
4 min read
jQuery TutorialjQuery is a lightweight JavaScript library that simplifies the HTML DOM manipulating, event handling, and creating dynamic web experiences. The main purpose of jQuery is to simplify the usage of JavaScript on websites. jQuery achieves this by providing concise, single-line methods for complex JavaSc
8 min read
Front End
React TutorialReact is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
Angular TutorialAngular is a powerful, open-source web application framework for building dynamic and scalable single-page applications (SPAs). Developed by Google, Angular provides a comprehensive solution for front-end development with tools for routing, form handling, HTTP services, and more.Designed for buildin
4 min read
Backend
Node.js TutorialNode.js is a powerful, open-source, and cross-platform JavaScript runtime environment built on Chrome's V8 engine. It allows you to run JavaScript code outside the browser, making it ideal for building scalable server-side and networking applications.JavaScript was mainly used for frontend developme
4 min read
Express.js TutorialExpress.js is a minimal and flexible Node.js web application framework that provides a list of features for building web and mobile applications easily. It simplifies the development of server-side applications by offering an easy-to-use API for routing, middleware, and HTTP utilities.Built on Node.
4 min read
PHP TutorialPHP is a popular, open-source scripting language mainly used in web development. It runs on the server side and generates dynamic content that is displayed on a web application. PHP is easy to embed in HTML, and it allows developers to create interactive web pages and handle tasks like database mana
8 min read
Laravel TutorialLaravel is an open-source PHP web application framework that has gained immense popularity since its inception in 2011, created by Taylor Otwell. This renowned framework empowers developers to build robust, scalable web applications with remarkable ease. As a developer-friendly framework, Laravel of
3 min read
Database
Web Technologies Questions The following Web Technologies Questions section contains a wide collection of web-based questions. These questions are categorized based on the topics HTML, CSS, JavaScript, and many more. Each section contains a bulk of questions with multiple solutions. Table of Content HTML QuestionsCSS Question
15+ min read