Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 以状态字符串呈现html_Javascript_Html_Reactjs - Fatal编程技术网

Javascript 以状态字符串呈现html

Javascript 以状态字符串呈现html,javascript,html,reactjs,Javascript,Html,Reactjs,我使用setState来更新部分html,但我发现新的更新html没有呈现。这是代码: html: 你好世界 js: var Main=React.createClass({ getInitialState:函数(){ 返回{output:'helloworld'}; }, render:function(){ 返回( {this.state.output} ); } }); React.renderComponent(,document.getElementById('content');

我使用setState来更新部分html,但我发现新的更新html没有呈现。这是代码:

html:

你好
世界
js:

var Main=React.createClass({
getInitialState:函数(){
返回{output:'hello
world
'}; }, render:function(){ 返回( {this.state.output} ); } }); React.renderComponent(,document.getElementById('content');
我省略了从服务器更新html的部分,但结果是一样的。如何呈现处于状态的字符串?

用于将HTML作为字符串注入React(但请注意,React将无法在其虚拟DOM中使用此标记):



用于执行此操作的API非常复杂,因为常规执行此操作不安全。React具有针对XSS的保护功能,使用
危险的LysetinerHTML
可以避免这些问题,如果您不清理这些数据,则会使您面临风险。

有效。谢谢还有一个问题,这是一种常见做法还是最佳做法?存储HTML并不常见,但有时是必要的(例如,您在服务器上从用户数据生成安全的HTML,如对堆栈溢出的注释,并需要对其进行渲染)。什么是清理此HTML的最佳方法?
<div>hello<br />world<br /></div>
<div id='content'></div>
var Main = React.createClass({
    getInitialState: function() {
        return {output: 'hello<br />world<br />'};
      },
    render: function() {
        return (
            <div>           
                {this.state.output}
            </div>
        );
        }
});

React.renderComponent(<Main/>, document.getElementById('content'));
<div dangerouslySetInnerHTML={{__html: this.state.output}} />