Redux - 在组件中调用调度操作后解析Promise
问题描述:
我有一个'addToFolder'操作,它分派在我的Redux Reducer中处理的操作。Redux - 在组件中调用调度操作后解析Promise
export function addToFolder(folderIds, contentId, contentType) {
const item = {};
item.folder_ids = folderIds;
item.content_id = contentId;
item.content_type = contentType;
return function(dispatch) {
return fetch(`${getAPIPath}api/addtofolder`, {
method: 'PUT',
body: JSON.stringify(item),
credentials: 'include',
headers: {
Accept: 'application/json',
},
})
.then((response) => {
response.json().then((res) => {
dispatch(addToFolderSuccess(res.data));
});
}).catch((error) => {
throw (error);
});
};
}
在我的组件中点击我的'添加到文件夹'按钮后,我发送操作。我想使用.then
来触发点心/通知操作,但使用我的API调用中的响应来填写通知显示的ID和消息文本。目前response
未定义。
addToFolder =() => {
this.props.actions.addToFolder(this.state.activeItems, this.props.contentId, this.props.contentType)
.then((response) => {
this.setState({ addBtnLoading: false });
this.props.actions.dispatch(showSnack(response.id, {
label: response.message,
timeout: 4000,
}));
});
};
答
你忘记返回
.then((response) => {
// nested promise
return response.json().then((res) => {
dispatch(addToFolderSuccess(res.data));
// the result
return res;
});