我想知道我是否有可能使用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
做的那样。这里有个例子。我没有完全按照你的代码做,但是大意是一样的。