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