reactjs - 在 Gatsby/Contentful 中使用 Lodash get
问题描述
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'?
解决方案
通过使用 lodash 中的“get”,它可以处理某些情况,this.props.data
即undefined
,如果您尝试访问this.props.data.contentfulBlogPost
.
您还可以使用this.props?.data?.contentfulBlogPost
推荐阅读
- python - Python3在每次循环后删除列表中的n个元素
- html - CSS 未应用于 html
- python - Python - 列表的字典 - 为每个唯一的列表元素找到关联的键
- spring-boot - 在外部任务处理程序中接收 Camunda 活动名称
- c# - c# 允许 list.add 中的重复项
- flutter - 通过添加 void 函数,它不会调用 statlessWidget 的 build 方法
- javascript - “flex: 1 1 0%” 带有适应其大小的画布子项无法正常工作
- nlog - NLog、ASP.NET Core 5.0、ClearProviders 和使用 OutputDebugString 的区别
- paramiko - 如何使用 paramiko 打开真正的终端窗口?
- .net - 如何使用 Oracle.DataAccess 安全地安装包