首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gatsby Redux combineReducers函数

Gatsby Redux combineReducers函数
EN

Stack Overflow用户
提问于 2020-04-16 14:35:43
回答 1查看 306关注 0票数 0

我正在学习Redux,在尝试使用Redux设置Gatsby时遇到了一个问题。我在使用createStore时遇到了这个错误:

代码语言:javascript
复制
C:/Users/wools/github/nutritionist-website/gatsby-site/node_modules/redux/es/redux.js:91 Uncaught (in promise) Error: Expected the reducer to be a function.
    at createStore (C:/Users/wools/github/nutritionist-website/gatsby-site/node_modules/redux/es/redux.js:91)
    at Module._default (C:/Users/wools/github/nutritionist-website/gatsby-site/ReduxWrapper.js:42)
    at eval (C:/Users/wools/github/nutritionist-website/gatsby-site/.cache/api-runner-browser.js:51)
    at Array.map (<anonymous>)
    at exports.apiRunner (C:/Users/wools/github/nutritionist-website/gatsby-site/.cache/api-runner-browser.js:39)
    at Module.eval (C:/Users/wools/github/nutritionist-website/gatsby-site/.cache/root.js:234)
    at eval (C:/Users/wools/github/nutritionist-website/gatsby-site/.cache/root.js:292)
    at Module../.cache/root.js (commons.js:1064)
    at __webpack_require__ (commons.js:726)
    at fn (commons.js:101)
    at eval (C:/Users/wools/github/nutritionist-website/gatsby-site/.cache/app.js:95)

按照官方redux文档的建议,我正在导出使用combineReducers()从reducers/index.js获得的单个reducer。我认为主要的问题是combineReducers()返回的是对象而不是函数。

ReduxWrapper.js

代码语言:javascript
复制
import React from 'react';
import { Provider } from 'react-redux';
import { getAllProducts } from './src/actions'
import { createStore, applyMiddleware } from 'redux'
import { createLogger } from 'redux-logger'
import thunk from 'redux-thunk'
import rootReducer from './src/reducers/';

const middleware = [ thunk ];
if (process.env.NODE_ENV !== 'production') {
  middleware.push(createLogger());
}

const store = createStore(
  rootReducer,
  applyMiddleware(...middleware)
)

store.dispatch(getAllProducts())

export default ({ element }) => (
  <Provider store={createStore()}>{element}</Provider>
);

组合的reducer位于文件reducers/index.js中:

代码语言:javascript
复制
import { combineReducers } from 'redux'
import cart, * as fromCart from './cart'
import products, * as fromProducts from './products'

const rootReducer = combineReducers({
  cart,
  products
})

const getAddedIds = state => fromCart.getAddedIds(state.cart)
const getQuantity = (state, id) => fromCart.getQuantity(state.cart, id)
const getProduct = (state, id) => fromProducts.getProduct(state.products, id)

export const getTotal = state =>
  getAddedIds(state)
    .reduce((total, id) =>
      total + getProduct(state, id).price * getQuantity(state, id),
      0
    )
    .toFixed(2)

export const getCartProducts = state =>
  getAddedIds(state).map(id => ({
    ...getProduct(state, id),
    quantity: getQuantity(state, id)
  }))

  export default rootReducer;

我的开发环境:

代码语言:javascript
复制
{
  "name": "gatsby-starter-default",
  "private": true,
  "description": "A simple starter to get up and developing quickly with Gatsby",
  "version": "1.0.0",
  "author": "David W",
  "dependencies": {
    "@fortawesome/fontawesome": "^1.1.8",
    "@fortawesome/fontawesome-free": "^5.12.1",
    "@fortawesome/fontawesome-svg-core": "^1.2.27",
    "@fortawesome/free-brands-svg-icons": "^5.12.1",
    "@fortawesome/free-solid-svg-icons": "^5.12.1",
    "@fortawesome/react-fontawesome": "^0.1.9",
    "@reach/router": "^1.3.3",
    "bootstrap": "^4.4.1",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "gatsby": "^2.19.45",
    "gatsby-background-image": "^0.10.2",
    "gatsby-image": "^2.2.43",
    "gatsby-plugin-env-variables": "^1.0.1",
    "gatsby-plugin-manifest": "^2.2.39",
    "gatsby-plugin-offline": "^3.0.32",
    "gatsby-plugin-react-helmet": "^3.1.23",
    "gatsby-plugin-sass": "^2.1.29",
    "gatsby-plugin-sharp": "^2.4.3",
    "gatsby-source-filesystem": "^2.1.46",
    "gatsby-transformer-sharp": "^2.3.13",
    "google-map-react": "^1.1.6",
    "jquery": "^3.4.1",
    "node-sass": "^4.13.1",
    "nodemailer": "^6.4.3",
    "prop-types": "^15.7.2",
    "react": "^16.13.1",
    "react-bootstrap": "^1.0.0-beta.17",
    "react-dom": "^16.13.1",
    "react-fontawesome": "^1.7.1",
    "react-helmet": "^5.2.1",
    "react-map-gl": "^5.2.3",
    "react-redux": "^5.1.2",
    "redux": "^4.0.5",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.3.0",
    "typescript": "^3.8.3"
  },
  "devDependencies": {
    "prettier": "^1.19.1"
  },
  "keywords": [
    "gatsby"
  ],
  "license": "MIT",
  "scripts": {
    "build": "gatsby build",
    "develop": "gatsby develop",
    "format": "prettier --write \"**/*.{js,jsx,json,md}\"",
    "heroku-postbuild": "gatsby build",
    "start": "gatsby serve",
    "serve": "gatsby serve",
    "clean": "gatsby clean",
    "test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/gatsbyjs/gatsby-starter-default.git"
  },
  "bugs": {
    "url": "https://github.com/gatsbyjs/gatsby/issues"
  },
  "homepage": "https://github.com/gatsbyjs/gatsby-starter-default#readme",
  "main": "gatsby-browser.js"
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-16 14:55:19

您可以使用以下内容修改ReduxWrapper.jsx并尝试吗?

代码语言:javascript
复制
import React from 'react';
import { Provider } from 'react-redux';
import { getAllProducts } from './src/actions'
import { createStore, applyMiddleware } from 'redux'
import { createLogger } from 'redux-logger'
import thunk from 'redux-thunk'
import rootReducer from './src/reducers/';

const middleware = [ thunk ];
if (process.env.NODE_ENV !== 'production') {
  middleware.push(createLogger());
}

const store = createStore(
  rootReducer,
  applyMiddleware(...middleware)
)

store.dispatch(getAllProducts())

export default ({ element }) => (
  <Provider store={store}>{element}</Provider>
);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61244104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档