我们使用store.dispath进行派发时,只能传递一个普通对象进去,如下:
store.dispatch({ type: ‘INCREMENT‘ })
但是,在使用redux-thunk中间件后,我们就可以传递一个函数进去
import { createStore, applyMiddleware } from ‘redux‘ import thunk from ‘redux-thunk‘ const store = createStore( reducer, applyMiddleware(thunk) ) store.dispatch(function (dispatch) { // ... which themselves may dispatch many times dispatch({ type: ‘INCREMENT‘ }) dispatch({ type: ‘INCREMENT‘ }) dispatch({ type: ‘INCREMENT‘ }) setTimeout(() => { // ... even asynchronously! dispatch({ type: ‘DECREMENT‘ }) }, 1000) })
在这个函数中,我们派发了多个action,甚至可以异步执行一些操作,比如延迟1000ms,派发action。那我们执行异步操作,就是通过这个中间件来实现的。
原文:https://www.cnblogs.com/MrSaver/p/10520254.html