【发布时间】:2017-04-25 13:44:24
【问题描述】:
我有一个 react/redux 应用程序,我面临以下挑战。当我点击一个按钮时,我想保存表单数据,如果后端发生验证错误,我想显示和消息,否则重定向。
在我的组件中我有这个功能
doSubmit = (data) => {
const {actions, linkUtils, myObject} = this.props
return actions.checkAuthentication()
.then(actions.sendMyObject(myObject, data) )
.then(linkUtils.openRoute('nextUrl'))
}
现在动作使用 dispatch 来调用 reducer,我可以验证 reducer 是否被调用。我使用动作类型,并在动作类型上使用开关盒:
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') } )
这样只有当对象保存成功时才会完成重定向。
我该怎么做?
【问题讨论】:
标签: javascript reactjs redux