【发布时间】:2017-10-15 20:25:03
【问题描述】:
我正在尝试将部分添加到调查对象及其引发状态突变错误。
这是我调用将整个调查对象作为参数的动作创建者的方法。
addNewSection(sectionName){
const id = performance.now();
let newSurvey = Object.assign({}, this.state.survey);
const section = Object.assign({},{
"type": "section",
"id": id,
"title": sectionName,
"position": {
"offset": 0,
"width": 12,
"order": 1
},
"items": []
});
newSurvey.layout.sections.push(section);
this.setState({survey: newSurvey});
this.props.actions.updateSurvey(newSurvey);
}
动作创建者:
export function updateSurvey(survey){
return {type:types.ADD_SECTION_SUCCESS, survey};
}
减速器:
export default function surveyReducer(state = initialState.survey, action){
switch(action.type){
case types.ADD_SECTION_SUCCESS:
return action.survey;
default:
return state
}
}
状态对象的形式为:
survey: {
layout: {
sections: [{},{},{}]
},
questions:[{},{},{}]
}
我一定是误解了 Object.assign。 Object.assign 是否会复制调查中的每个嵌套对象,如果我只是在调查对象的最顶层使用它,就像我在这里使用的一样?
【问题讨论】:
标签: javascript reactjs redux frontend mutation