React Redux Introduction when why how 2025
React Redux Introduction: When, Why, and How (2025)
React Redux is a state management library commonly used in large scale React applications. It allows you to manage global state outside of components and avoid prop drilling.
đ Why Use Redux?
- To share state globally across components
- To separate logic from UI
- To handle complex state logic in large applications
- To debug easily using Redux DevTools
đ When to Use Redux?
- When multiple components need access to the same state
- When prop drilling becomes a problem
- When you need centralized state logic
- When your app scales and has multiple developers
đ How Redux Works
- Store â holds the state
- Action â describes what happened
- Reducer â describes how state changes based on action
- Dispatch â sends action to reducer
- useSelector â access state in components
- useDispatch â send actions from components
â
Redux Setup Example
// store.js
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './features/counter/counterSlice';
export const store = configureStore({
reducer: {
counter: counterReducer,
},
});
// counterSlice.js
import { createSlice } from '@reduxjs/toolkit';
export 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;
// App.js
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { increment, decrement } from './features/counter/counterSlice';
function App() {
const count = useSelector((state) => state.counter.value);
const dispatch = useDispatch();
return (
Count: {count}
);
}
export default App;
đĻ Wrap Your App with Provider
// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { Provider } from 'react-redux';
import { store } from './store';
ReactDOM.render(
,
document.getElementById('root')
);
đ With this setup, you now have a global state management solution using Redux in your React app.