redux-thunk

安装

  1. yarn add redux-thunk

配置

  1. // store/index.js
  2. import { createStore, applyMiddleware, compose } from 'redux'
  3. import reducer from './reducer' // 管理员
  4. import thunk from 'redux-thunk'
  5. const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
  6. ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({})
  7. : compose
  8. const enhancer = composeEnhancer(applyMiddleware(thunk)) // 增强函数,链式调用。为了兼容 devtool
  9. const store = createStore(
  10. reducer,
  11. // window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() // redux
  12. // applyMiddleware(thunk) // thunk
  13. enhancer
  14. )
  15. export default store

方法二:

https://www.npmjs.com/package/redux-devtools-extension

  1. import { createStore, applyMiddleware } from 'redux'
  2. import thunk from 'redux-thunk'
  3. import { composeWithDevTools } from 'redux-devtools-extension'
  4. import reducers from './reducers'
  5. const store = createStore(reducers, composeWithDevTools(applyMiddleware(thunk)))
  6. export default store