首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:无法使用NextJS读取未定义的Redux的属性getState

错误:无法使用NextJS读取未定义的Redux的属性getState
EN

Stack Overflow用户
提问于 2019-09-13 23:01:55
回答 1查看 2.6K关注 0票数 3

我正在尝试用redux制作一个nextjs应用程序,我正在设置最基本的部分。它在这里如下所示。

server.js

代码语言:javascript
复制
const express = require("express");
const nextjs = require("next");
const { ROUTES, ROUTE_PATHS } = require("./utils/constants");

const dev = process.env.NODE_ENV !== "production";
const PORT = process.env.PORT || 3001;

const app = nextjs({ dev });
const handle = app.getRequestHandler();

app
  .prepare()
  .then(() => {
    const server = express();

    server.get(ROUTES.HOME_ROUTE, (req, res) => {
      app.render(req, res, ROUTE_PATHS.HOME_ROUTE_PATH, {});
    });

    server.get("*", (req, res) => handle(req, res));
    server.listen(PORT, err => {
      if (err) throw err;
      console.log(`> Ready on http://localhost:${PORT}/`);
    });
  })
  .catch(e => {
    console.error(e.stack);
    process.exit(1);
  });

_app.js

代码语言:javascript
复制
import React from "react";
import { Provider } from "react-redux";
import App from "next/app";
import withRedux from "next-redux-wrapper";

import configureStore from "./../redux/store";

class Application extends App {
  static async getInitialProps({ Component, ctx }) {
    const pageProps = Component.getInitialProps
      ? await Component.getInitialProps(ctx)
      : {};
    return { pageProps };
  }
  render() {
    const { Component, pageProps, store } = this.props;
    return (
      <Provider store={store}>
        <Component {...pageProps} />
      </Provider>
    );
  }
}

export default withRedux(configureStore)(Application);

store.js

代码语言:javascript
复制
import { createStore, applyMiddleware, compose } from "redux";
import rootReducer from "./rootReducer";

export default (initialState = {}, options) => {
  let composeEnhancers = compose;
  if (process.env.NODE_ENV !== "production" && typeof window === "object") {
    if (window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) {
      composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({});
    }
    const middlewares = [];
    const enhancers = [applyMiddleware(...middlewares)];
    return createStore(
      rootReducer,
      initialState,
      composeEnhancers(...enhancers)
    );
  }
};

每当我尝试运行客户端时,都会收到错误消息,

代码语言:javascript
复制
Cannot read property 'getState' of undefined
TypeError: Cannot read property 'getState' of undefined
    at Object.<anonymous> (/Users/sriram/Desktop/boilerplate/client/node_modules/next-redux-wrapper/lib/index.js:155:75)
    at step (/Users/sriram/Desktop/boilerplate/client/node_modules/next-redux-wrapper/lib/index.js:56:23)
    at Object.next (/Users/sriram/Desktop/boilerplate/client/node_modules/next-redux-wrapper/lib/index.js:37:53)
    at fulfilled (/Users/sriram/Desktop/boilerplate/client/node_modules/next-redux-wrapper/lib/index.js:28:58)

当我设置redux的时候,我觉得一定是出了什么问题,但是我不知道该在哪里解决这个问题。既然没有任何适当的堆栈跟踪,还有没有人遇到过这个问题?我该如何修复它?

EN

回答 1

Stack Overflow用户

发布于 2019-09-13 23:21:18

您的createStore函数可能有问题。

代码语言:javascript
复制
export default (initialState = {}, options) => {
  let composeEnhancers = compose;

  // FIXME: it returns undefined in some cases
  if (process.env.NODE_ENV !== "production" && typeof window === "object") {
    if (window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) {
      composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({});
    }
    const middlewares = [];
    const enhancers = [applyMiddleware(...middlewares)];
    return createStore(
      rootReducer,
      initialState,
      composeEnhancers(...enhancers)
    );
  }
};

你最好用下面的方式来写:

代码语言:javascript
复制
export default (initialState = {}, options) => {
  let composeEnhancers = compose;

  if (
    process.env.NODE_ENV !== "production" 
    && typeof window === "object"
    && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
  ) {
    composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({});
  }

  const middlewares = [];
  const enhancers = [applyMiddleware(...middlewares)];

  return createStore(
    rootReducer,
    initialState,
    composeEnhancers(...enhancers)
  );
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57926058

复制
相关文章

相似问题

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