Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

javascript - React.js转换成HTML(Reactjs convert to html)

I'm having trouble dealing with facebook's ReactJS.

(我在处理Facebook的ReactJS时遇到问题。)

Whenever I do ajax and want to display an html data, ReactJS displays it as text.

(每当我执行ajax并想要显示html数据时,ReactJS会将其显示为文本。)

(See figure below)

((见下图))

ReactJS渲染字符串

The data is displayed through the success callback function of the jquery Ajax.

(通过jquery Ajax的成功回调函数显示数据。)

$.ajax({
   url: url here,
   dataType: "json",
   success: function(data) {
      this.setState({
           action: data.action
      })
   }.bind(this)
});

在此处输入图片说明

Is there any easy way to convert this into html?

(有没有简单的方法可以将其转换为html?)

How should I do it using ReactJS?

(我应该如何使用ReactJS?)

  ask by Peter Wateber translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By default, React escapes the HTML to prevent XSS (Cross-site scripting) .

(默认情况下,React会转义HTML以防止XSS(跨站脚本) 。)

If you really want to render HTML, you can use the dangerouslySetInnerHTML property:

(如果您确实要呈现HTML,则可以使用dangerouslySetInnerHTML属性:)

<td dangerouslySetInnerHTML={{__html: this.state.actions}} />

React forces this intentionally-cumbersome syntax so that you don't accidentally render text as HTML and introduce XSS bugs.

(React强制使用这种故意繁琐的语法,这样您就不会意外地将文本呈现为HTML并引入XSS错误。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...