简体   繁体   English

Next.js 在 getServerSideProps 方法中访问 Page props

[英]Next.js access Page props inside the getServerSideProps method

I'm passing some properties to the on the _app page.我正在将一些属性传递给_app页面上的 。

<PageWrapper>
  <Component someProps={someProps} />
  <GlobalCSS />
 </PageWrapper>

Is it possible to access these props in the getServerSideProps method on each Page?是否可以在每个页面的 getServerSideProps 方法中访问这些道具?

export const getServerSideProps: GetServerSideProps = async context => {
  const data = await fetchData(someProps); // ??
  
  return { props: { data } };
};

Lets say you have a page demo with假设您有一个页面demo

// ...
export const getServerSideProps = async () => {
  const demo = await fetch('/api/demo');

  return {
    props: {
      demo,
    }
  };
};

and somewhere in your other child component you can access it您可以在其他子组件的某个地方访问它

export const MyComponent = () => {
  const { components } = useRouter();

  console.log(components['demo'].props.pageProps);
};

Attention!注意力! This is undocumented and hacky way.这是无证和hacky的方式。

You can check this Next JS document fetching data and use.您可以查看这个Next JS文档获取数据并使用。 I hope so this is usefull for you !我希望这对你有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM