问题描述
所以我正在构建一个博客帖子,这个JSON文件的整个帖子中都包含HTML,例如< p>< / p>
或< br>
等。在我的ReactApp中放置它的最有效方法是什么?我可以毫不费力地获取其他所有内容,但不知道如何处理'post'部分,因为不同的文章可能有< br>
等等在不同的领域并且希望如此避免使用dangerouslySetInnerHTML或至少找到一种安全的使用方式。
So I'm currently building a blogpost and the entire 'post' of this JSON file contains HTML in it, things like <p></p>
or <br>
and such. What's the most efficient way to place this in my ReactApp? I have no trouble getting everything else in but have no clue how to handle the 'post' part since different articles may have <br>
and such in different areas and would like to avoid using dangerouslySetInnerHTML or at least find a safe way of using it.
推荐答案
你可以使用在通过 dangerouslySetInnerHTML
将其插入DOM之前清理HTML。希望它有效!
You can use DOMPurify to sanitize your HTML before inserting it in the DOM via dangerouslySetInnerHTML
. Hope it works!
这篇关于Reactjs dangerouslySetInnerHtml替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!