redux-thunk学习

107 阅读1分钟

redux-thunk有什么用

redux-thunk是一个redux的中间件,用来处理redux中的复杂逻辑,比如异步请求;

redux-thunk中间件可以让action创建函数先不返回一个action对象,而是返回一个函数;

解决Redux Devtool和Redux-thunk同时引用造成的冲突

import { createStore, applyMiddleware, compose } from 'redux'
import reducer from './reducer'
import thunk from 'redux-thunk'   // 引入redux-thunk

//Redux Devtools配置
const composeEnhancers = 
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? 
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({}) : compose;

// 将Redux Devtools和redux-thunk合并
const enhancer = composeEnhancers(applyMiddleware(thunk));

const store = createStore(
    reducer,
    enhancer
);

export default store;