最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Connecting NextJS, next-i18next, with-redux, with-redux-saga: "Error: If you have a getInitialProps method

programmeradmin11浏览0评论

I'm trying to connect a functioning NextJS/React app that uses 'with-redux-saga' and 'with-redux' to 'next-i1iN' () -- but when my app boots I get the following error:

Error: If you have a getInitialProps method in your custom _app.js file, you must explicitly return pageProps. For more infor mation, see: .js#custom-app

TypeError: Cannot read property 'namespacesRequired' of undefined at Function.getInitialProps (/Users/cerulean/Documents/Projects/PAW-React/node_modules/next-i18next/dist/hocs/app-with-translation.js:94:57) at process._tickCallback (internal/process/next_tick.js:68:7)

But I am returning page props in my _app.js.

// _app.js
 static async getInitialProps({ Component, ctx }) {

    const pageProps = {};
    let temp;

    if (Component.getInitialProps) {
      temp = await Component.getInitialProps({ ctx });
    }
    Object.assign(pageProps, temp);
    return { ...pageProps };
  }

Perhaps there is something wrong with how I am hooking together the various HOCs? In _app.js I have:

export default withRedux(createStore, { debug: false })(withReduxSaga({ async: true })(i18nInstance.appWithTranslation(MyApp)));

And in my index.js I have:

   // index.js
const mapStateToProps = (state) => ({ homeData: getHomePageData(state) });

export default connect(mapStateToProps)(withNamespaces('mon')(Index));

Any insights much appreciated!

I'm trying to connect a functioning NextJS/React app that uses 'with-redux-saga' and 'with-redux' to 'next-i1iN' (https://github./isaachinman/next-i18next) -- but when my app boots I get the following error:

Error: If you have a getInitialProps method in your custom _app.js file, you must explicitly return pageProps. For more infor mation, see: https://github./zeit/next.js#custom-app

TypeError: Cannot read property 'namespacesRequired' of undefined at Function.getInitialProps (/Users/cerulean/Documents/Projects/PAW-React/node_modules/next-i18next/dist/hocs/app-with-translation.js:94:57) at process._tickCallback (internal/process/next_tick.js:68:7)

But I am returning page props in my _app.js.

// _app.js
 static async getInitialProps({ Component, ctx }) {

    const pageProps = {};
    let temp;

    if (Component.getInitialProps) {
      temp = await Component.getInitialProps({ ctx });
    }
    Object.assign(pageProps, temp);
    return { ...pageProps };
  }

Perhaps there is something wrong with how I am hooking together the various HOCs? In _app.js I have:

export default withRedux(createStore, { debug: false })(withReduxSaga({ async: true })(i18nInstance.appWithTranslation(MyApp)));

And in my index.js I have:

   // index.js
const mapStateToProps = (state) => ({ homeData: getHomePageData(state) });

export default connect(mapStateToProps)(withNamespaces('mon')(Index));

Any insights much appreciated!

Share Improve this question edited Mar 20, 2019 at 0:30 Rotem jackoby 22.3k14 gold badges141 silver badges139 bronze badges asked Feb 7, 2019 at 13:29 CeruleanCerulean 6,03311 gold badges72 silver badges123 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

For anyone ing to this issue and wondering what @cerulean meant in his answer.

1) use require instead of import

NextJS doesn't transpile your modules if you use a custom server (more info). Therefore you cannot use import in your next-i18next configuration without going through a vale of tears.

// NextI18NextConfig.js

const NextI18Next = require('next-i18next/dist/monjs')

module.exports = new NextI18Next({
  defaultLanguage: "en",
  otherLanguages: ["de"]
  // ... other options
});
// server.js

const nextI18next = require("./path/to/NextI18NextConfig");

// ... the rest of your server.js code

This is a mix&match from next-i18next example and documentation


2) keep pageProps as it is

You cannot play too much with getInitialProps returned value. If you need to add extra stuff you should be careful not replacing or manipulating pageProps. See below.

  static async getInitialProps({ Component, ctx }) {
    let pageProps = {}

    const extraStuff = doSomeExtraStuff()

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx)
    }

    return { pageProps, extraStuff }
  }

More about it on this thread.

If you have config redux sagas correctly, this way work for me:

export default withRedux(configureStore)(withReduxSaga(appWithTranslation(MyApp)));

Two things for those who encounter a similar situation.

1) When they say 'return pageProps', it means 'return pageProps', not '...pageProps'

2) I was using ES6 import statements in the file which set up the 'next-i18next' singleton. Needed to use 'require' and 'module.exports'

Now it works...

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论