两种方法都需要使用redux提供的applyMiddleware函数
第一种方法:使用applyMiddleware包装 createStore产生一个新的创建store 的函数。(实际上是一个Store Enhancer)
例:
import {createStore , applyMiddleware} from ’ redux ’ J
import thunkMiddleware from ’ redux- thunk ’
const configureStore = applyMiddleware (thunkMiddleware) (createStore);
const store= configureStore (reducer , initialState) ;
第二种方法:同样把applyMiddleware结果当做Store Enhancer,和其他Enhancer混合之后作为 createStore的参数传入。
import {createStore , applyMiddleware , compose} from ' redux ’;
import thunkMiddleware from ’ redux- thunk ’
const win = window;
const storeEnhancers =compose (
applyMiddleware (. . .middlewares ),
(win && win .devToolsExtension) ? win. devToolsExtension () : f => f
const store= createStore (reducer , storeEnhancers );