Next-js组件使用泛型类型传递异步数据

Next-js组件使用泛型类型传递异步数据

定义类型

1
2
3
4
5
type PageProps = {
hello: string,
page: any,
random: any
}

返回属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 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
const Home: NextPage<PageProps> = (props, context) => {
return (
<p>{JSON.stringify(props.page)}</p>
)
}
作者

Terwer

发布于

2022-08-01

更新于

2022-08-01

许可协议

评论