未捕获的错误:期望增强器是一个功能

Gor*_*ath 0 reactjs redux react-redux

我试图从组件调用reducer并想在component中进行渲染,但是当我试图将reduce存储在redux的createStore()方法中时,上面的错误就要来了。我的代码是这样的:

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
    )
  )
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:未捕获的错误消息:期望增强器是一个功能

the*_*ode 6

您正在将两个reducer传递给createStore函数,而不是一个。

由于createStore的第三个参数始终是增强器函数,因此它认为'initiaeState'变量是增强器函数,因为您将其作为thid参数传递给createStore。

createStore期望接收以下参数:

  1. reducer(函数):一种归约函数,返回给定当前状态树和要处理的操作,返回下一个状态树。
  2. [preloadedState](任意):初始状态。您可以选择指定它,以在通用应用程序中混合服务器中的状态,或还原以前序列化的用户会话。如果您使用CombineReducers生产了reducer,则它必须是形状与传递给它的键相同的普通对象。否则,您可以自由传递减速器可以理解的所有内容。

  3. [增强器](功能):商店增强器。您可以选择指定它来增强第三方功能,例如中间件,时间旅行,持久性等。Redux随附的唯一商店增强器是applyMiddleware()。

请记住,应用程序中的根减速器应将所有减速器组合为一个减速器。

来自Redux文档

首先,最重要的是要了解整个应用程序实际上仅具有一个reducer功能