简体   繁体   English

如何在Redux反应存储中更改多个状态项

[英]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. 我在redux初始状态中存储了多个值,并且有一个动作处理程序StepOneFn正在接受组件赋予它的更改的所有值。 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. 现在,它仅根据有效负载更改城市和地址,而其他未更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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