我有一个react/redux应用程序,我面临以下挑战。当我单击按钮时,我想保存表单数据,如果后端发生验证错误,我想显示消息并以其他方式重定向。
在我的组件中我有这个功能
doSubmit = (data) => {
const {actions, linkUtils, myObject} = this.props
return actions.checkAuthentication()
.then(actions.sendMyObject(myObject, data) )
.then(linkUtils.openRoute('nextUrl'))
}
现在该操作使用调度来调用 reducer ,我可以验证 reducer 是否被调用。我使用操作类型,并在操作类型上使用 switch case:
case types.SAVE_SUCCESS:
return {
...state,
save: true,
}
case types.SAVE_ERROR:
return {
..state,
saved: false,
fieldErrors: action.payload.errors
}
现在在 doSubmit 中我想要这样的东西:
return actions.checkAuthentication()
.then(actions.sendMyObject(myObject, data) )
.then( if (saved) { linkUtils.openRoute('nextUrl') } )
只有当对象保存成功时,才会完成重定向。
我该怎么做?
最佳答案
您可以打开sendMyObject
返回的数据:
return actions.checkAuthentication()
.then(actions.sendMyObject(myObject, data) )
.then((saved) => {
if (saved) {
return linkUtils.openRoute('nextUrl');
} else {
// something else
}
});
要实现此功能,您的 Promise sendMyObject
需要解析 saved
或其他一些信息,告诉链中的下一个 Promise 是否成功。
关于javascript - React/redux - Onclick 对 reducer 使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43612438/