question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'getState' of undefined

See original GitHub issue

Error:

TypeError: Cannot read property 'getState' of undefined
    at /home/leo/tiki_clone/tiki_fe/node_modules/react-redux/lib/components/Provider.js:31:18
    at Object.useMemo (/home/leo/tiki_clone/tiki_fe/node_modules/react-dom/cjs/react-dom-server.node.development.js:1582:19)
    at useMemo (/home/leo/tiki_clone/tiki_fe/node_modules/react/cjs/react.development.js:1532:21)
    at Provider (/home/leo/tiki_clone/tiki_fe/node_modules/react-redux/lib/components/Provider.js:30:42)
    at processChild (/home/leo/tiki_clone/tiki_fe/node_modules/react-dom/cjs/react-dom-server.node.development.js:3353:14)
    at resolve (/home/leo/tiki_clone/tiki_fe/node_modules/react-dom/cjs/react-dom-server.node.development.js:3270:5)
    at ReactDOMServerRenderer.render (/home/leo/tiki_clone/tiki_fe/node_modules/react-dom/cjs/react-dom-server.node.development.js:3753:22)
    at ReactDOMServerRenderer.read (/home/leo/tiki_clone/tiki_fe/node_modules/react-dom/cjs/react-dom-server.node.development.js:3690:29)
    at renderToString (/home/leo/tiki_clone/tiki_fe/node_modules/react-dom/cjs/react-dom-server.node.development.js:4298:27)
    at Object.renderPage (/home/leo/tiki_clone/tiki_fe/node_modules/next/dist/next-server/server/render.js:53:851)
    at Function.getInitialProps (/home/leo/tiki_clone/tiki_fe/.next/server/pages/_document.js:293:19)
    at Function.getInitialProps (/home/leo/tiki_clone/tiki_fe/.next/server/pages/_document.js:883:85)
    at loadGetInitialProps (/home/leo/tiki_clone/tiki_fe/node_modules/next/dist/next-server/lib/utils.js:5:101)
    at renderToHTML (/home/leo/tiki_clone/tiki_fe/node_modules/next/dist/next-server/server/render.js:53:1142)
    at async /home/leo/tiki_clone/tiki_fe/node_modules/next/dist/next-server/server/next-server.js:99:97
    at async /home/leo/tiki_clone/tiki_fe/node_modules/next/dist/next-server/server/next-server.js:92:142

image

_app.js

import React, { Fragment, useEffect } from 'react';
import Head from 'next/head';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import homeReducer from '../src/reducers/home';
import { createWrapper } from 'next-redux-wrapper';

const makeStore = () => {
  return createStore(homeReducer);
};

function _App(props) {
  const { Component, pageProps, store } = props;

  return (
    <React.Fragment>
      <Head>
        <title>Tiki</title>
        <meta
          name='viewport'
          content='minimum-scale=1, initial-scale=1, width=device-width'
        />
      </Head>
      <Provider store={store}>
        <Component {...pageProps} />
      </Provider>
    </React.Fragment>
  );
}

_App.getInitialProps = async ({ Component, ctx }) => {
  const pageProps = Component.getInitialProps
    ? await Component.getInitialProps(ctx)
    : {};

  return { pageProps };
};

const wrapper = createWrapper(makeStore);

export default wrapper.withRedux(_App);

reducers/home.js

import { HYDRATE } from 'next-redux-wrapper';

const initialState = {};

const homeReducer = (state = { ...initialState }, action) => {
  switch (action.type) {
    case HYDRATE:
      return {
        bac: 'xc'
      };
    default:
      return {
        okk: 'noooo'
      };
  }
};

export default homeReducer;

_document.js

export default class _Document extends Document {
  static async getInitialProps(ctx) {
    const sheets = new ServerStyleSheets();
    const originalRenderPage = ctx.renderPage;

    const initialProps = await Document.getInitialProps(ctx);

    ctx.renderPage = () =>
      originalRenderPage({
        enhanceApp: (App) => (props) => sheets.collect(<App {...props} />)
      });

    return {
      ...initialProps,
      style: [
        ...React.Children.toArray(initialProps.styles),
        sheets.getStyleElement()
      ]
    };
  }

  render() {
    return (
      <Html>
        <Head>
          <link
            rel='stylesheet'
            type='text/css'
            href='http://localhost:3000/icons/flaticon.css'
          />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

Sorry about so many codes. However, I see it’s possible that error occurs in _document.js Thanks in advance!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
amrsekillycommented, Dec 28, 2021

Thank you so much, @lioaslan!! 🙏🏻

1reaction
lioaslancommented, Dec 28, 2021

I removed <Provider> on _app.js, you can see it in the codesandbox image

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read property 'getState' of undefined
TypeError : Cannot read property 'getState' of undefined. So I changed it to this, and it works (it finds getState from redux) (CORRECT):...
Read more >
Cannot read property 'getState' of undefined (Testing with Jest ...
I'm trying to mock out the store with an initial state, then supplying it to StoreProvider . Tab component is hooking into the...
Read more >
Cannot read property 'getState' of undefined error-Reactjs
Coding example for the question Cannot read property 'getState' of undefined error-Reactjs.
Read more >
HTML : Cannot read property 'getState' of undefined error
HTML : Cannot read property ' getState' of undefined error [ Gift : Animated Search Engine : https://bit.ly/AnimSearch ] HTML : Cannot read ......
Read more >
接入react-redux 之后运行出现错误TypeError: Cannot read ...
接入react-redux 之后运行出现错误TypeError: Cannot read property 'getState' of undefined at Provider.js:19 ... 出现这个错误,原因是因为 store 并没有 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found