我正在尝试连接一个功能良好的NextJS/React应用程序,它使用“with-redux-saga”和“with-redux”到“next-i1iN”(https://github.com/isaachinman/next-i18next) --但是当我的应用程序启动时,我会发现以下错误:
错误:如果您的自定义_app.js文件中有一个_app.js方法,则必须显式返回pageProps。有关更多信息,请参见:https://github.com/zeit/next.js#custom-app (/Users/cerulean/Documents/Projects/PAW-React/node_modules/next-i18next/dist/hocs/app-with-translation.js:94:57) at TypeError: TypeError:无法读取未定义的属性“namespacesRequired”( process._tickCallback / Function.getInitialProps /next_tick.js:68:7)
但我正在返回我的_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 };
}
也许我把不同的HOCs联系在一起有什么问题吗?在_app.js
中,我有:
export default withRedux(createStore, { debug: false })(withReduxSaga({ async: true })(i18nInstance.appWithTranslation(MyApp)));
在我的index.js
里,我有:
// index.js
const mapStateToProps = (state) => ({ homeData: getHomePageData(state) });
export default connect(mapStateToProps)(withNamespaces('common')(Index));
任何见解都非常感谢!
发布于 2019-03-19 22:33:12
对于任何来到这个问题并想知道@cerulean在他的回答中的含义的人来说。
1)使用require
而不是import
如果您使用自定义服务器( NextJS ),则NextJS不会传输您的模块。因此,您不能在下一个-i18Next配置中使用import
,而不需要经历一段时间的失败。
// NextI18NextConfig.js
const NextI18Next = require('next-i18next/dist/commonjs')
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
这是来自next-i 18 next 示例和文档的混合&匹配
2)保持pageProps
的原样
您不能过多地使用getInitialProps
返回的值。如果您需要添加额外的内容,您应该小心,不要替换或操作pageProps
。见下文。
static async getInitialProps({ Component, ctx }) {
let pageProps = {}
const extraStuff = doSomeExtraStuff()
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}
return { pageProps, extraStuff }
}
在这条线上有更多关于它的信息。
发布于 2020-02-13 05:20:45
如果您的配置redux sagas是正确的,这种方法适用于我:
export default withRedux(configureStore)(withReduxSaga(appWithTranslation(MyApp)));
发布于 2019-02-07 16:32:40
对于遇到类似情况的人来说,有两件事。
1)当他们说‘返回页面支持’时,意思是‘返回页面支持’,而不是'...pageProps‘
2)我在文件中使用了ES6导入语句,该语句设置了“Next-i18Next”单例。需要使用“要求”和“module.exports”
现在起作用了。
https://stackoverflow.com/questions/54574471
复制相似问题