【发布时间】:2017-06-23 19:38:50
【问题描述】:
这就是我试图用组件的render()-方法中的元素替换一些字符串的方式。但这失败了,因为我将替换的字符串作为真正的字符串而不是元素。
我必须做什么才能将Link 作为渲染 Link-元素?现在它只是一个字符串输出。
这是正确的“反应”方式吗?
return (
<List>
{
elements.map(e => {
return (
<List.Item>
{
links ? links.map(link => {
return e.content.replace(
new RegExp(link.label, 'gi'),
'<Link to="/' + link.id + '">$&</Link> (<Icon name="external" />)'
)
}) : ''
}
</List.Item>
)
})
}
</List>
)
元素
[{
"_id" : "zQS6pXicvXk7K2rZ4",
"content" : "This is a sample text to add some links",
"links" : [
{
"id" : "Dn59y87PGhkJXpaiZ",
"type" : "articles",
"label" : "Sample"
},
{
"id" : "GhkJXpaiZDn59y87P",
"type" : "articles",
"label" : "add"
},
{
"id" : "XpaiZDn5GhkJ9y87P",
"type" : "articles",
"label" : "External"
}
]
}]
【问题讨论】:
-
elements通常看起来像什么?还有links? -
都是对象数组。
-
我是从你的代码中得到的,但这有助于查看示例数据。
-
我明白了。需要根据
labels将content拆分成一个字符串数组,如["This is a", "sample", "text to", "add", "some links"],然后将数组元素替换为<Link />s。
标签: javascript reactjs