我正在使用redux,而我的动作之一(ADD_DESIGN)却非常奇怪地更新了状态。
应该将对象(我检查了action.payload,一切都很好)添加到array(state.designs)中。但是它以某种方式进行管理,不仅可以添加该对象,还可以从state.design_sets.designs添加一些其他对象,并对其进行推送。请查看图片...如果有人知道可能导致这种行为的原因,请帮忙。
State, and Actions being dispatched
case ADD_DESIGN:
return {
...state,
designs: [...state.designs, action.payload]
};
我在此处分派动作:
export const addDesign = (id, file, designData, previewData) =>
dispatch => {
dispatch(setFileUpload(previewData));
axios
.get("/api/file-upload/generatepresignedurl")
.then(res => {
if (res.data.success) {
const fileuploadurl = res.data.urls[0];
axios.put(fileuploadurl, file, config).then(() => {
axios.post(`/api/designs/${id}`, postData).then(design => {
dispatch({
type: ADD_DESIGN,
payload: design.data
}),
dispatch({
type: CLEAR_FILE_UPLOAD,
payload: previewData.id
});
});
});
}
})
UPLOAD减速器与“设计”等分开(我正在使用组合减速器)...这些动作可能会改变状态,并引起问题吗?
---------- 已解决 -----------
在我的用于上载文件组件,我有这样的:
const { designs, design_sets } = this.props;
let fullDesignList = designs;
design_sets.forEach(design_set => {
design_set.designs.forEach(design => {
fullDesignList.push(design);
});
});
仍然不确定这怎么会弄乱状态,但确实如此?