Next.JS组件中的SSR



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

  • index.js
    • view.js
      • part.js (Class component)

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

我已经尝试在getServerSideProps中传递一个函数作为组件的道具,但它不允许。

我可以运行服务器端渲染代码在反应类组件?

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

如果你想在页面/文件上使用它,但它不起作用,检查2

  1. 如果你有_app。
  2. 如果你有__app。你应该像这样定义它
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;