const promiseAction = store => next => action => { const {type, playload} = action; if (playload && typeof playload.then === 'function') { playload.then(result => { store.dispatch({type, playload: result}); }).catch(e => {}) } else { next(action); } } action = { type: 'xxx', playload: Ajax(url) }
自定义中间件:很多时候网上的redux中间件可能不太符合项目中的需要,所以这时候可以自己写一套适合项目的中间件,以下指示本博主的一个demo,形式不唯一:
export const PromiseWares = store => next => action => { next({type: 'right', playload: 'loading'}); if (typeof action === 'function') { const {dispatch} = store; action(dispatch); } else { const {type, playload} = action; if (playload && typeof playload.then === 'function') { playload.then(result => { store.dispatch({type, playload: result}); }).catch(e => {}) } else { next(action); next({type: 'right', playload: 'noLoading'}); } } };
以上就是本博主对redux中间件的理解,如有不对,请指出。
您可能感兴趣的文章: