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

Terwer...小于 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