5

在此处输入图像描述

在这个项目中,我将下一个 js 库与 redux 一起使用,并且我还使用下一个 redux 包装器我按照下一个 redux 包装器文档中提到的所有操作,但它在这里无法正常工作,它会出现 getState of undefined 错误,因为我显示在上图。所以在这里我调试了所有代码,但我找不到出现这个错误的地方。我在下面列出了我在我的 react js 文件中完成的代码。

_app.js

import React from "react";
import App from "next/app";
import { Provider } from "react-redux";
import withRedux from "next-redux-wrapper";
import makeStore from ".././src/config/store";

class ConsoleApp extends App {
  /**
   * Render View
   */
  render() {
    const { Component, pageProps, store } = this.props;
    return (
      <Provider store={store}>
        <Component {...pageProps} />
      </Provider>
    );
  }
}

export default withRedux(makeStore)(ConsoleApp);

根减速器.js

/**
 * Import Dependencies
 */
import { combineReducers } from "redux-immutable";

/**
 * Import Reducers
 * All Reducers used in the App must be declared here!
 */
import GraphicReducer from "../modules/Graphics/reducer";
import UserReducer from "../modules/User/reducer";
/**
 * Combine the Reducers
 */
const reducers = combineReducers({
  graphics: GraphicReducer,
  user: UserReducer
});

/**
 * Export the combined Reducers
 */
export default reducers;

store.js

import { createStore, applyMiddleware } from "redux";
import { composeWithDevTools } from "redux-devtools-extension";
import reducers from "./root-reducer";
import thunk from "redux-thunk";
/**
 * Prepare the Redux Store
 */

const composedMiddlewares = applyMiddleware(thunk);

const storeEnhancers = composeWithDevTools({
  name: "React-node-test"
})(composedMiddlewares);

const makeStore = () => {
  createStore(reducers, storeEnhancers);
};

export default makeStore;

4

1 回答 1

6

请使用以下代码段,因为您忘记返回createStore (reducers, storeEnhancers);

const makeStore = () => {
  return createStore(reducers, storeEnhancers);
};
于 2020-01-04T05:37:01.847 回答