reactjsreduxreact-reduxnext.jsnext-redux-wrapper

Next redux wrapper arise error of Cannot read property 'getState' of undefined


enter image description here

In this project, I use the next js library along with redux and I also use next redux wrapper I do everything as mentioned in the next redux wrapper document but it is not working properly here it arises the getState of undefined error as I display on the above image. So here I debugged all the code but I could not found the point from where this error is arising. I listed the code below which I have done in my react js files.

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

root-reducer.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;


Solution

  • Please use the below snippet as you forgot to return the createStore(reducers, storeEnhancers);

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