![](/img/trans.png)
[英]How to render array entries in JSX using map() function with conditional wrapping
[英]how to render an array of JSX using .map in react
我有一组如下所示的对象:
const features = [
{
name: "Sponsor Block",
images: [sponsorBlock1, sponsorBlock2],
data: [
`Uses the API found ${<a href='example.com/'>here</a>}. You can find more information on how it works`,
"Lorem ipsum ........"
],
},
]
在第一点你可以看到我正在使用模板字符串来注入 JSX(我不确定这是否是正确的方法)
我想 map (循环)遍历特征数组并显示其中带有锚标记的第一个点,以及其他只是文本的点作为普通文本。
但是当我遍历第一个 object 中的数据键时,我将其显示在我的 JSX-> [Object][Object] 中
有没有办法解决这个问题,同时仍将其保留在数组中?
为什么不只是一个片段组件数组
data:[
<>Uses the API found <a href='example.com/'>here</a>. You can find more information on how it works</>,
<>Lorem ipsum ........</>
]
然后当你渲染时:
return (
<ul>
{features.data.map((d,i)=><li key={i}>{d}</li>}
</ul>
)
const features = [ { name: "Sponsor Block", images: [sponsorBlock1, sponsorBlock2], data: [ `Uses the API found <a href='example.com/'>here</a>. You can find more information on how it works`, "Lorem ipsum ........" ], }, ] features.map(feature => { return <div dangerouslySetInnerHTML={{ __html: feature.data }} /> })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.