Next.js with Typescript -如何在功能组件中访问app.js的props和staticProps&



我想知道我是否有可能使用typescript和功能组件访问从app.js传递到所有组件页的道具。看看app.js和index.ts

app.js:

function MyApp({ Component, pageProps, router }) {
const [message, setMessage] = useState(false)
return (
<Component setMessage={setMessage} key={router.route} {...pageProps} />
)
}
export default MyApp

Index.ts:

function Home({products}): InferGetStaticPropsType<typeof getStaticProps>) {
return (
<div>Home</div>
)
}
export function getStaticProps(){
const products = []
}

我希望能够访问setMessage函数和索引中的getStaticProps产品。ts页面。我可能做错了,谢谢你的建议。

这是可能的,就像普通组件一样。把你的道具和pageProps一起传递,就像你已经对setMessage做的那样。这里有个例子。我没有完全按照你的代码做,但是大意是一样的。

最新更新