【问题标题】:Use of Lodash get in Gatsby/Contentful在 Gatsby/Contentful 中使用 Lodash get
【发布时间】:2021-03-16 07:18:16
【问题描述】:

Gatsby/Contentful Starter 代码示例:

import get from 'lodash/get'
    ...
    const post = get(this.props, 'data.contentfulBlogPost')
    const siteTitle = get(this.props, 'data.site.siteMetadata.title')

为什么他们在html代码中使用lodash'get'而不是直接引用graphql-query结果'data'?

【问题讨论】:

    标签: reactjs graphql lodash gatsby contentful


    【解决方案1】:

    通过使用 lodash 中的“get”,它可以处理 this.props.dataundefined 的某些情况,以避免在尝试访问 this.props.data.contentfulBlogPost 时代码引发错误。

    您也可以使用this.props?.data?.contentfulBlogPost

    【讨论】:

      猜你喜欢
      • 2020-09-30
      • 2021-11-20
      • 2021-12-16
      • 2020-03-20
      • 2021-01-15
      • 2020-08-18
      • 2022-10-22
      • 2019-07-13
      • 2021-07-14
      相关资源
      最近更新 更多