简体   繁体   中英

How can I mutate multiple state items in redux-react store

I have multiple values stored in my redux initial state, and I have one action handler StepOneFn that is taking in all the values my component has given it to change. I want to update the initial state to change only some of the values. I'm not sure how to go about doing it.

let initial_state = {
    house_name:"",
    address:"",
    city:"",
    state:"",
    zip:0,
    img:"",
    mortgage:0,
    rent:0
}
const step_one = "step_one"

export default function reducer(state = initial_state,action){
switch(action.type){
    default:
        return state;
    case step_one:
        return {...state,...action.payload}
    }
}

export function StepOneFn(name,address,city,state,zip){
return{
    type:step_one,
    payload:{
        name,
        address,
        city,
        state,
        zip
    }
  }
}

If you want to change only some values you can do like this

case step_one:
        return {...state, address:action.payload.address, city:action.payload.city}
    }

Now it only changes city and address as per payload others are unchanged.

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