combineReducers
The `combineReducers` function allows splitting the Redux state into multiple slices, each managed by its own reducer. It combines these reducers into a single root reducer.
Key concepts
combineReducers
A function that allows splitting the Redux state into multiple slices, each managed by its own reducer. Combines these reducers into a single root reducer.
Examples
combineReducers Example:
import { combineReducers, createStore } from 'redux';
const userReducer = (state = { user: null }, action) => {
switch (action.type) {
case 'SET_USER':
return { ...state, user: action.payload };
default:
return state;
}
};
const counterReducer = (state = { count: 0 }, action) => {
switch (action.type) {
case 'INCREMENT':
return { count: state.count + 1 };
default:
return state;
}
};
// Combine reducers
const rootReducer = combineReducers({
user: userReducer,
counter: counterReducer
});
// Create store
const store = createStore(rootReducer);
console.log(store.getState()); // { user: { user: null }, counter: { count: 0 } }Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus