简体   繁体   中英

How do i update object in redux

I would like to update an object in the middle of processing redux I need to check if this is first time for a function.

here is my code.

const initialState = {
  table: [],
  timer: 0,
  result: "",
  gameState: false,
  openedCount: 0,
  isFirst: true
};

what I tried

if(state.isFirst) {
   state.isFirst = false;
}

How do I update like that in redux? just want to update for isFirst not returning state.

I will return {...state, table: newArray }

I would really appreciate your help thanks for reading my question.

you can write this condition:

if(state.isFirst) {
   state.isFirst = false;
}

in each switch case: and update your state accordingly.

For example:

const initialState = {
  table: [],
  timer: 0,
  result: "",
  gameState: false,
  openedCount: 0,
  isFirst: true
};
const Reducer(state=initialState,action)=>{
   switch(action.type){
      case "first" : 
           if(state.isFirst) {
              state.isFirst = false;
              // update the new state
           }       
      case "second" : 
           if(state.isFirst) {
              state.isFirst = false;
              // update the new state
           }   
      default : return;        
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM