我正在用MERN堆栈构建一个非常基本的博客类型的应用程序,并将其开发到可以发布一篇非常简单的文本文章的程度。有人能建议我如何解析该字符串以在我的内容中呈现HTML吗?
所以帖子看起来像
粗体文本
具有
a link
而不是
<b> Bold Text with <a href="#"> a link</a></b>
我的一个想法是尝试使用
DOMParser
在组件中,类似于
const parser = new DOMParser();
const parsedContent = parser.parseFromString(post.description, 'text/html');
// and then in the render just print it out
<div className='content'>
{parsedContent}
</div>
这当然不起作用,因为这是一个完整的回报
html
对象和React错误
Objects are not valid as a React child.. if you meant to render a collection of children, use an array instead.
解析该字符串并在我的内容中呈现HTML的正确方法是什么?