问题描述
我正在尝试从组件调用 reducer 并希望在 component 中呈现它,但是当我尝试将 reducer 存储在 redux 的 createStore() 方法中时,就会出现上述错误.我的代码是这样的:-
import { applyMiddleware, compose, createStore } from 'redux'从redux-thunk"导入 thunk从反应路由器"导入 { browserHistory }从 './reducers' 导入 makeRootReducer从 './location' 导入 { updateLocation }从 './reducers' 导入 allReducers;导出默认值 (initialState = {}) =>{//======================================================//中间件配置//======================================================const 中间件 = [thunk]//======================================================//存储增强器//======================================================常量增强器 = []让 composeEnhancers = 撰写如果(__DEV__){const composeWithDevToolsExtension = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__if (typeof composeWithDevToolsExtension === '函数') {composeEnhancers = composeWithDevToolsExtension}}//======================================================//存储实例化和 HMR 设置//======================================================const store = createStore(所有减速机,makeRootReducer(),初始状态,composeEnhancers(应用中间件(...中间件),...增强剂))
我收到错误:未捕获的错误:预期增强器是一个功能
您将两个 reducer 传递给 createStore 函数,而不是一个.
由于 createStore 的第三个参数始终是增强器函数,因此它认为 'initiaeState' 变量是一个增强器函数,因为您将它作为第三个参数传递给 createStore.
createStore 期望接收以下参数:
reducer (Function):一个reducer函数,返回下一个状态树,给定当前状态树和要处理的操作.
[preloadedState] (any):初始状态.您可以选择指定它以从通用应用程序中的服务器中获取状态,或恢复以前序列化的用户会话.如果你生产带有 combineReducers 的 reducer,这必须是一个带有与传递给它的键相同的形状.否则,您可以自由通过任何你的减速器能理解的东西.
[enhancer](功能):商店增强器.您可以选择指定它以使用第三方功能增强商店,例如作为中间件,时间旅行,持久化等.唯一的存储Redux 附带的增强器是 applyMiddleware().
请记住,应用中的根 reducer 应该将所有 reducer 合并为一个 reducer.
来自 Redux 文档
首先,重要的是要了解您的整个应用实际上只有一个reducer函数
import { applyMiddleware, compose, createStore } from 'redux'
import thunk from 'redux-thunk'
import { browserHistory } from 'react-router'
import makeRootReducer from './reducers'
import { updateLocation } from './location'
import allReducers from './reducers';
export default (initialState = {}) => {
// ======================================================
// Middleware Configuration
// ======================================================
const middleware = [thunk]
// ======================================================
// Store Enhancers
// ======================================================
const enhancers = []
let composeEnhancers = compose
if (__DEV__) {
const composeWithDevToolsExtension = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
if (typeof composeWithDevToolsExtension === 'function') {
composeEnhancers = composeWithDevToolsExtension
}
}
// ======================================================
// Store Instantiation and HMR Setup
// ======================================================
const store = createStore(
allReducers,
makeRootReducer(),
initialState,
composeEnhancers(
applyMiddleware(...middleware),
...enhancers
)
)
You are passing in two reducers to the createStore function instead of one.
Since the third argument to createStore is always the enhancer function it thinks that the 'initiaeState' variable is an enhancer function since you are passing this in as the thid argument to createStore.
createStore expects to receive the following arguments:
Remember the root reducer in your app should combine all your reducers into one single reducer.
From the Redux docs
这篇关于未捕获的错误:期望增强器是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!