【发布时间】:2017-11-17 14:55:01
【问题描述】:
请帮助我,使用 redux thunk 进行异步调度是否正确?
//actions types
const ADD_ARTIST = 'ADD_ARTIST'
// action
function AddArtist(val){
return {
type: ADD_ARTIST,
payload:val
}
}
//some async events
function async1(id){
return fetch(.....)
}
function async2(id){
return fetch(.....)
}
function async3(id){
return fetch(.....)
}
function auth(id) {
return function (dispatch) {
async1(10).then( (v) => dispatch(AddArtist(v)) );
Promise.all([
async2(26),
async3(51),
]).then(
(v) => dispatch(AddArtist(v[0] + v[1] )
));
}
}
var ViewWrapper = connect(
state => ({
playlist: state.playlist,
artist: state.artist,
}),
dispatch => (
{
dispatch,
auth: bindActionCreators(auth, dispatch)
})
)(View);
和按钮:
<input type='button' onClick={() =>
this.props.dispatch( this.props.auth() ) } value='dispatch mapDispatch>>>>>>>' />
【问题讨论】:
-
根据documentation,这看起来是正确的。但你也需要错误处理。
标签: redux redux-thunk