【发布时间】:2020-09-03 06:29:36
【问题描述】:
我正在尝试查看某个用户是否在帖子中被提及。如果是这样,我想将用户名链接到个人资料。
我尝试了什么:
if (mentions.length === 1) {
const splitSearch = '@' + mentions[0].user.name;
const replaceWith =
'<a href=profile/' +
mentions[0].user._id +
'>@' +
mentions[0].user.name +
'</a>';
newText = text.split(splitSearch).join(replaceWith);
}
...
return (
...
{mentions.length > 0 && (
<p id='text-container'>{newText}</p>
)}
但我只是将标签作为纯文本返回:
hi <a href=profile/5f1bd6c7d90cb03e845adbbf>@user1</a>.
hello <a href=profile/5f30380288a63e001755401e>@user2</a>.
我是否需要以另一种方式编写replaceWith const?我尝试了Link 而不是a,但无法正常工作。
提前致谢!
【问题讨论】: