兩種方法都需要使用redux提供的applyMiddleware函數(shù)
第一種方法:使用applyMiddleware包裝 createStore產(chǎn)生一個(gè)新的創(chuàng)建store 的函數(shù)签财。(實(shí)際上是一個(gè)Store Enhancer)
例:
import {createStore , applyMiddleware} from ’ redux ’ J
import thunkMiddleware from ’ redux- thunk ’
const configureStore = applyMiddleware (thunkMiddleware) (createStore);
const store= configureStore (reducer , initialState) ;
第二種方法:同樣把a(bǔ)pplyMiddleware結(jié)果當(dāng)做Store Enhancer,和其他Enhancer混合之后作為 createStore的參數(shù)傳入举农。
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 );