【问题标题】:Next.JS SSR in component组件中的 Next.JS SSR
【发布时间】:2021-11-04 22:53:41
【问题描述】:

我想集成一个只支持服务器端渲染的模块。这是项目结构。

  • index.js
    • view.js
      • part.js(类组件)

我可以在 index.js 的 getServerSideProps 方法中使用模块。但我想在 part.js 中使用它,因为我需要将一些内容传递给 part.js 中可用的模块。

我尝试将 getServerSideProps 中的函数作为道具传递给组件,但不允许。

我可以在 react 类组件中运行服务端渲染代码吗?

【问题讨论】:

标签: javascript reactjs next.js server-side-rendering react-component


【解决方案1】:

服务器端渲染只在 pages/ 目录上工作,你不能在组件或 .... 中使用它。

如果你想在页面/文件上使用它并且它不工作检查 2 件事

  1. 您的项目中是否有 _app.jsx
  2. 如果你有 __app.jsx 你应该像这样定义它
import App from 'next/app';

const MyApp = ({ Component, pageProps }) => {

    return (
        <Component {...pageProps} />
    );
};

MyApp.getInitialProps = async (appContext) => {
    const { pageProps } = await App.getInitialProps(appContext);
    const { ctx } = appContext;
    return { pageProps};
};

export default MyApp;

【讨论】:

    猜你喜欢
    • 2021-12-02
    • 2021-07-27
    • 2021-07-05
    • 2019-12-27
    • 2022-06-12
    • 2023-02-17
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    相关资源
    最近更新 更多