从 getServerSideProps 传递到 Page 的 props 始终是未定义的

Jer*_*eid 6 next.js

我正在使用 nextjs 9.3.5,即使是最简单的 getServerSideProps 示例也总是失败:

function Page({ data })
{
    //Prints undefined
    console.log(data);

    return <div>Data in props: {data}</div>
}

export async function getServerSideProps()
{
    var data = "Hello";

    //Prints "Hello"
    console.log(data);

    return { props: { data } };
}

export default Page
Run Code Online (Sandbox Code Playgroud)

这基本上是从 nextjs 网站上非常简单的示例中剪切和粘贴的。getInitialProps 工作正常。

Mat*_*eri 12

如果您根据需要添加和内部的官方文档_app.js将文件添加到项目中,这里是文件的最小实现。ComponentpageProps_app.js

function MyApp({ Component, pageProps }) {
   return <Component {...pageProps} />
}

export default MyApp
Run Code Online (Sandbox Code Playgroud)


Tim*_*ung -3

这应该位于您作为 prop 返回的 json 结构中。

尝试这个:

export async function getServerSideProps() {
  const data = {title:"Hello Sir"};
  return { props:  data }
}
Run Code Online (Sandbox Code Playgroud)