user6557921
user6557921

Reputation: 55

How to return state of Redux reducers

For redux reducer step : What if I only want to change one single property of initial_state. For example:

const INITIAL_STATE = {
  signInInfo: {
    signin: false,
     name: "",
      email: "",
     ...
  },
  changePassword: {
    status: false,
     ...
  }
  ...
};

Here I only want to set signInInfo.signin as true, currently , the only way I know is to input a complete "signInInfo" like :

case SIGNIN_USER:
    return { ...state, signInInfo: action.payload.data };

action.payload.data is like:

{
   signin: true,
   name: "Qing",
   email : ...
}

And another question is what if I want to set signInInfo.signin as false and meanwhile also need to change changePassword.status from false to true. What should I do? Can anyone give me a hint?

Upvotes: 4

Views: 5656

Answers (1)

DDS
DDS

Reputation: 4375

Fairly simple (both questions):

case SIGNIN_USER: return {
    ...state,
    signInInfo: {
      ...state.signInInfo,
      signin: action.payload.signin
    },
    changePassword: {
      ...state.changePassword,
      status: true
    }
  };

Upvotes: 4

Related Questions