graphql - 使用 gatsby-source-contentful 使用 Gatsbyjs 和 Contentful 渲染富文本
问题描述
我正在使用 gatsby-source-contentful v4.6.4
根据文档,我应该能够毫无问题地使用类似于以下的查询(来自文档的简化查询):
query MyQuery {
contentfulBlogPost {
id
bodyRichText {
raw
}
}
}
连同 documentToReactComponents。但是,以下代码不会引发错误,只是不会解析富文本:
function ArticleBody({raw}) {
return (
<>
{
raw.map(rtNode => {
return (
<>
{documentToReactComponents(rtNode)}
</>
)
})
}
</>
);
}
export default ArticleBody;
解决方案
问题是我需要解析 JSON,并将对象降级到内容数组中。
这是组件:
function ArticleBody({raw}) {
return (
<>
{
JSON.parse(raw).content.map(rtNode => {
return (
<>
{documentToReactComponents(rtNode)}
</>
)
})
}
</>
);
}
export default ArticleBody;
这是 GraphQL 查询:
query{
allContentfulBlog {
edges {
node {
id
blogPost {
raw
}
}
}
}
}
推荐阅读
- spring-data-jpa - I am getting Weird output in console when fetching Data from Database
- html - CSS 在不显示时被阻止
- python - 在python中计算IRR函数的循环问题
- hyperledger-fabric - 超级账本:错误:无法从创世区块创建账本:账本 [mychannel] 已存在,状态为 [ACTIVE]
- javascript - Child component not updating when parent state changes
- php - 如何将当前图像编号添加到 WooCommerce 中的每个产品库图像?
- java - 我试图了解如何使用 2D 数组
- javascript - 映射具有可变数量字段的嵌套对象?
- testing - Dependabot 是否进行测试以确保它不会破坏构建?
- haskell - 从 Windows 10 中彻底删除 Stack