REDUX: Who creates state?

REDUX: Who creates state?
0

#1

Hello there,

I’m finishing the Redux module and just completed the Redux Counter Challenge after a little help checking old forum posts but I’m having a little trouble figuring something out: who creates state?

I understand store is where the state lives and reducer affects state given certain actions but how is state created? Right now my best guess is that state is created in the reducer parameters (I believe that’s what I did in my counter) but in that case, what if I wanted to have state saving 2 different counters? How could I do such a thing?

Here’s my counter reducer:

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

Thanks in advance


#2

you can create a state object. For example

const initialState = {
    counter1: 0,
    counter2: 0
}

then in the parameters of the reducer instead of

const counterReducer = (state = 0, action) =>{

you can set the state as the state object you have defined

const counterReducer = (state = initialState, action) =>{

#3

Thanks! I think I get it now