未捕获的错误:期望增强器是一个函数 [英] Uncaught Error: Expected the enhancer to be a function

查看:14
本文介绍了未捕获的错误:期望增强器是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

我正在尝试从组件调用 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 期望接收以下参数:

<块引用>

  1. reducer (Function):一个reducer函数,返回下一个状态树,给定当前状态树和要处理的操作.

  2. [preloadedState] (any):初始状态.您可以选择指定它以从通用应用程序中的服务器中获取状态,或恢复以前序列化的用户会话.如果你生产带有 combineReducers 的 reducer,这必须是一个带有与传递给它的键相同的形状.否则,您可以自由通过任何你的减速器能理解的东西.

  3. [enhancer](功能):商店增强器.您可以选择指定它以使用第三方功能增强商店,例如作为中间件,时间旅行,持久化等.唯一的存储Redux 附带的增强器是 applyMiddleware().

请记住,应用中的根 reducer 应该将所有 reducer 合并为一个 reducer.

来自 Redux 文档

<块引用>

首先,重要的是要了解您的整个应用实际上只有一个reducer函数

I am trying to call the reducer from the component and want to render that in component , but when I am trying to store the reducer in the createStore() method of redux above error is coming. My Code is like this:-

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
    )
  )

I am getting error :Uncaught Error: Expected the enhancer to be a function

解决方案

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:

  1. reducer (Function): A reducing function that returns the next state tree, given the current state tree and an action to handle.

  2. [preloadedState] (any): The initial state. You may optionally specify it to hydrate the state from the server in universal apps, or to restore a previously serialized user session. If you produced reducer with combineReducers, this must be a plain object with the same shape as the keys passed to it. Otherwise, you are free to pass anything that your reducer can understand.

  3. [enhancer] (Function): The store enhancer. You may optionally specify it to enhance the store with third-party capabilities such as middleware, time travel, persistence, etc. The only store enhancer that ships with Redux is applyMiddleware().

Remember the root reducer in your app should combine all your reducers into one single reducer.

From the Redux docs

First and foremost, it's important to understand that your entire application really only has one single reducer function

这篇关于未捕获的错误:期望增强器是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆