Next-js组件使用泛型类型传递异步数据
# Next-js组件使用泛型类型传递异步数据
定义类型
type PageProps = {
hello: string,
page: any,
random: any
}
1
2
3
4
5
2
3
4
5
返回属性
// 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()
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
使用
const Home: NextPage<PageProps> = (props, context) => {
return (
<p>{JSON.stringify(props.page)}</p>
)
}
1
2
3
4
5
2
3
4
5
编辑 (opens new window)
上次更新: 2023/09/19, 13:33:19