0
我有一个动作的创造者是从我的阵营组件调用:从行动的创建者返回无极阵营本地使用终极版 - 咚
// ...
import { connect } from 'react-redux';
// ...
import { submitProfile } from '../actions/index';
// ...
onSubmit() {
const profile = {
name: this.state.name
// ...
};
this.props.submitProfile(profile)
.then(() => { // I keep getting an error here saying cannot read property 'then' of undefined...
console.log("Profile submitted. Redirecting to another scene.");
this.props.navigator.push({ ... });
});
}
export default connect(mapStateToProps, { submitProfile })(MyComponent);
行动的创建者的定义是类似于下面的东西。注意我正在使用redux-thunk中间件。
export function submitProfile(profile) {
return dispatch => {
axios.post(`some_url`, profile)
.then(response => {
console.log("Profile submission request was successful!");
dispatch({ ... }); // dispatch some action
// this doesn't seem to do anything . . .
return Promise.resolve();
})
.catch(error => {
console.log(error.response.data.error);
});
};
}
我希望能够做的就是调用行动的创建者提交的个人资料,然后后请求成功,推动了新的途径进入从我的组件导航。我只是希望能够确定发布的请求是成功的,所以我可以推送路线;否则,我不会推任何东西,但说出现错误,请重试。
我在网上查找并找到Promise.resolve(),但它似乎并没有解决我的问题。我知道,如果我使用了redux-promise中间件,我可以在调用动作创建器之后执行。我如何使用redux-thunk?
好吧,似乎现在工作。我只需要为axios.post请求添加** return **关键字。那就是:return dispatch => {** return ** axios.post(...); }; – nbkhope
您可以回答自己的问题并接受它。也许这会帮助其他人在未来遇到类似的问题。 –
其实我没有找到解决方案,因为我想处理组件中的错误情况,当我调用动作创建器时,但事实证明它总是返回成功案例,即使在动作创建者本身执行了catch 。也许我需要在动作创建者的catch块内部返回Promise.reject来完成这项工作? – nbkhope