簡體   English   中英

Next.js 在 getServerSideProps 方法中訪問 Page props

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

我正在將一些屬性傳遞給_app頁面上的 。

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

是否可以在每個頁面的 getServerSideProps 方法中訪問這些道具?

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

假設您有一個頁面demo

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

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

您可以在其他子組件的某個地方訪問它

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

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

注意力! 這是無證和hacky的方式。

您可以查看這個Next JS文檔獲取數據並使用。 我希望這對你有用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM