作用
redux的dispatch默认只能传入一个对象参数:
// 获取动态数据
export const getListAction = (data) => ({
type: GET_LIST,
data,
})
redux-thunk的作用是使dispatch支持函数参数:
componentDidMount() {
store.dispatch(getTodoList())
}
redux-thunk的安装和配置
安装
yarn add redux-thunk
配置
import { createStore, applyMiddleware, compose } from 'redux' // 引入createStore方法
import reducer from './reducer'
import thunk from 'redux-thunk'
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({}) : compose
const enhancer = composeEnhancers(applyMiddleware(thunk))
const store = createStore(reducer, enhancer) // 创建数据存储仓库
export default store //暴露出去