My simple way to combine multiple Reducers

Tell us what’s happening:
Describe your issue in detail here.
This is the simple and understandable way to combine multiple reducers to me

  **Your code so far**

const INCREMENT = 'INCREMENT';
const DECREMENT = 'DECREMENT';
const increaseCount=()=>{return {type:INCREMENT}};
store.dispatch(increaseCount);
const decreaseCount=()=>{return{type:DECREMENT}};
store.dispatch(decreaseCount);
const counterReducer = (state = 0, action) => {
switch(action.type) {
  case INCREMENT:
    return state + 1;
  case DECREMENT:
    return state - 1;
  default:
    return state;
}
};

const LOGIN = 'LOGIN';
const LOGOUT = 'LOGOUT';
const logingIn=()=>{return{type:LOGIN}};
store.dispatch(logingIn);
const logingOut=()=>{return{type:LOGOUT}};
store.dispatch(logingOut);
const authReducer = (state = {authenticated: false}, action) => {
switch(action.type) {
  case LOGIN:
    return {
      authenticated: true
    }
  case LOGOUT:
    return {
      authenticated: false
    }
  default:
    return state;
}
};

const rootReducer = Redux.combineReducers({count:counterReducer,
auth:authReducer});// Define the root reducer here

const store = Redux.createStore(rootReducer);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36

Challenge: Combine Multiple Reducers

Link to the challenge:

Yeah, I mean scanning it, that looks basically right. You’ll end up with a state tree that gives you state.auth.authenticated and state.count. If that’s what you want (no reason not to) then that’s great.

The only difference is that IRL, you would put each of those reducers in separate files (and often the types and action creators in their own files) and then import the reducers into a different file where you combine them. Often you state structure can be a complex tree structure with multiple layers of reducers, using combineReducers on all the nodes of that tree. Often you will have your folder structure mimic the structure of the tree.

But that is all in the future. For what you appear to be wanting, this look basically right.

1 Like

Thanks bro, I think that advice is going to help me.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.