找到你要的答案

Q:How to render formatted text in ReactJS webapp?

Q:如何在reactjs webapp渲染格式化文本?

I need to save, retrieve, render and edit formatted text in my webapp.

For example, I edit text with markddown syntax on the clientSide. I can convert it to html or save as it is. But how should i render this text when webapp receiving it as a string?

var formattedTextFromDataBase = "<p>some <em>formatted</em> text</p>"

I know about "dangerouslysetinnerhtml" - this method seemed to be a bad idea. Is there another way to deal with formatted text?

我需要保存、检索、呈现在我的网页编辑格式化文本。

For example, I edit text with markddown syntax on the clientSide. I can convert it to html or save as it is. But how should i render this text when webapp receiving it as a string?

var formattedTextFromDataBase = "<p>some <em>formatted</em> text</p>"

我知道“dangerouslysetinnerhtml”这种方法似乎是一个坏主意。是否有另一种处理格式化文本的方法?

answer1: 回答1:

No. If you want to render html provided from an external source, dangerouslySetInnerHTML was created for that purpose. As long as the source of the html is trusted (as is the case if users are only seeing their own html rendered), it should be fine.

如果你想使从外部源提供的HTML,dangerouslysetinnerhtml是为此目的创建。只要在HTML源是可信的(如用户如果只看到自己的HTML渲染),它应该被罚款。

reactjs