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