【问题标题】:Map function doesn't render elements地图功能不渲染元素
【发布时间】:2020-06-17 17:34:42
【问题描述】:

我从 Next.js 开始,我正在尝试从 CMS 呈现一些数据。我可以访问地图函数中的数据,但我不能在其中渲染任何元素。这是我的 index.tsx

type OpenJobsProps = {
    slug: string,
    image: string
}

const JoinUs = ({ jobs }: InferGetStaticPropsType<typeof getStaticProps>) => {
    return(
        <Layout>
            <Welcome>
                <h1>Join us</h1>
                <p>It’s designed to appeal to emotions, in order to<br/> achieve rational goals. The concept doesn’t make<br /> logical sense at first.</p>
            </Welcome>
            <OpenJobs>
                {jobs.data.map((item: any, index: number) => {
                    {console.log(item)}
                    <>
                        <OpenJobItem slug={item.slug} source={item.image}/> 
                        <p>{item.slug}</p>
                    </>
                })}
            </OpenJobs>
            <Culture/>
        </Layout>
    )
}

export const getStaticProps: GetStaticProps = async (ctx) => {
    const jobs: OpenJobsProps[] = (await getAllOpenJobs() || [])

    return {
      props: {
        jobs,
      }
    }
  }

export default JoinUs;

控制台日志返回 但 map 函数不返回任何元素或组件。

【问题讨论】:

标签: javascript reactjs typescript next.js


【解决方案1】:

添加return

{jobs.data.map((item: any, index: number) => {
    console.log(item);
    return (<>
        <OpenJobItem slug={item.slug} source={item.image}/> 
        <p>{item.slug}</p>
    </>);
})}

或者只是:

{
    jobs.data.map((item: any, index: number) => (<>
        <OpenJobItem slug={item.slug} source={item.image} />
        <p>{item.slug}</p>
    </>))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 2021-05-25
    • 2020-09-17
    • 2017-10-21
    • 2020-10-12
    • 1970-01-01
    • 2020-04-27
    相关资源
    最近更新 更多