使用jsx语法映射数组



我通过数组映射遇到麻烦,所以我可以将评论添加到评论结构化数据,JSX是无效的。我想知道这是否是完美的方式,或者我需要在调用它之前在函数中这样做!

<MetaTags>
{ this.state.name ? (
<script type="application/ld+json">
{`
{
"@context": "https://schema.org/",
"@type": "LocalBusiness",
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "${this.state.rating}",
"ratingCount": "${this.state.voters}"
},
"review": [
{
"@type": "Review",
"author": "Ellie",
"datePublished": "2011-04-01",
"reviewBody": "The lamp burned out and now I have to replace it.",
"name": "Not a happy camper",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "1",
"worstRating": "1"
}
},
{
"@type": "Review",
"author": "Ellie",
"datePublished": "2011-04-01",
"reviewBody": "The lamp burned out and now I have to replace it.",
"name": "Not a happy camper",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "1",
"worstRating": "1"
}
}
]
}
`}
</script>
) : ''}
</MetaTags>

我尝试用{' '}包装映射函数

"review": [
{` 
this.state.posts ? this.state.posts.slice(0, 10).map(data => (
{
"@type": "Review",
"author": "Ellie",
"datePublished": "2011-04-01",
"reviewBody": "The lamp burned out and now I have to replace it.",
"name": "Not a happy camper",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "1",
"worstRating": "1"
}
},
)) : '';
`}
]
"review": this.state.posts ? this.state.posts.slice(0, 10).map(data => ({
"@type": "Review",
"author": "Ellie",
"datePublished": "2011-04-01",
"reviewBody": "The lamp burned out and now I have to replace it.",
"name": "Not a happy camper",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "1",
"worstRating": "1"
}
})) : []
<MetaTags>
{this.state.name ? (
<script type="application/ld+json">
{`
{
"@context": "https://schema.org/",
"@type": "LocalBusiness",
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "${this.state.rating}",
"ratingCount": "${this.state.voters}"
},
"review": ${
this.state.posts
? this.state.posts.slice(0, 10).map((data) => ({
"@type": "Review",
author: "Ellie",
datePublished: "2011-04-01",
reviewBody:
"The lamp burned out and now I have to replace it.",
name: "Not a happy camper",
reviewRating: {
"@type": "Rating",
bestRating: "5",
ratingValue: "1",
worstRating: "1",
},
}))
: []
}
}
`}
</script>
) : (
""
)}
</MetaTags>;

最新更新