我正在使用redux,并且我的状态中包含空字符串值。每个空键都必须使用输入onChange事件来填写。假设我要更改“ business_selected”键,该函数应该是什么样?这是我的状态:
const initialState = {
user: {},
applications: [
{
companyFlow: {
stage1: {
business_activity: "",
business_selected: ""
},
stage2: {
legal_name: "",
registration_number: "",
business_website: "",
incorporation_date_text: "",
legal_form: ""
}
}
}
]
};
export default function changeInput(state = initialState, action) {
switch (action.type) {
case CHANGE_INPUT:
return {
// ??????
};
default:
return state;
}
}
onChnage
值应以本地状态而不是Redux状态存储。理想情况下,仅全局值应以redux状态存储。
通常onChange
是使用component
本身的本地状态来处理的,但是如果要在redux
中进行操作,则可以执行类似的操作。
export default function changeInput(state = initialState, action) {
switch (action.type) {
case CHANGE_INPUT:
return {
...state,
applications: state.applications.map(application => {
return Object.assign({}, application, {
...application,
companyFlow: {
...application.companyFlow,
stage1: {
...application.companyFlow.stage1,
business_selected: action.payload
}
}
}
return application
});
default:
return state;
}
}