Next-js组件使用泛型类型传递异步数据
...小于 1 分钟
Next-js组件使用泛型类型传递异步数据
定义类型
type PageProps = {
hello: string,
page: any,
random: any
}
返回属性
// https://github.com/vercel/next.js/blob/canary/examples/cms-wordpress/pages/index.js
export const getStaticProps: GetStaticProps<PageProps> = async (context) => {
const pageId = "20220718142548-vtf8mdm"
let page = await getPage(pageId)
if (!page) {
page = {}
}
return {
props: {
hello: 'world',
page: page,
random: Math.random()
}
}
}
使用
const Home: NextPage<PageProps> = (props, context) => {
return (
<p>{JSON.stringify(props.page)}</p>
)
}
Powered by Waline v2.14.9