Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.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 如何将React组件(或原始html)传递到其他React组件并插入它?_Javascript_Html_Reactjs_React Jsx - Fatal编程技术网

Javascript 如何将React组件(或原始html)传递到其他React组件并插入它?

Javascript 如何将React组件(或原始html)传递到其他React组件并插入它?,javascript,html,reactjs,react-jsx,Javascript,Html,Reactjs,React Jsx,我有从ajax返回的原始html标记。在React中呈现原始html并不容易。我试着上下使用他们的危险的HTML,但没有成功,只有React会抛出错误。这一点非常模糊 所以我决定将我的原始html编译成React组件并插入它。原始html被编译成React组件,但我不能插入它。这是我的密码。我想将组件(或者原始html更好)作为this.props.b传递到组件: var FooComponent = React.createClass({ render: function() {

我有从ajax返回的原始html标记。在React中呈现原始html并不容易。我试着上下使用他们的
危险的HTML
,但没有成功,只有React会抛出错误。这一点非常模糊

所以我决定将我的原始html编译成React组件并插入它。原始html被编译成React组件,但我不能插入它。这是我的密码。我想将
组件(或者原始html更好)作为
this.props.b
传递到
组件:

var FooComponent = React.createClass({
    render: function() {
            var Html = React.createClass({
                render: function() {
                    return (
                        myRawHtmlMarkup
                    );
                }
            });

        return (
            <Tr id={key} a={value} b={Html} />
        );
    }
});

Html
组件中的
render
方法将抛出错误,因为组件需要返回有效的
ReactComponent
,而不是字符串

我认为您可以在
componentDidMount
中解析HTML并作为子项追加:

var Tr=React.createClass({
componentDidMount:function(){
var div=document.createElement('div');
div.innerHTML=this.props.b;
this.refs.b.appendChild(div.firstChild)
},
render:function(){
返回(
{this.props.id}
{this.props.a}
);
}
});
ReactDOM.render(
,
document.querySelector('table tbody')
);

Html变量是否需要react类

如果没有,请尝试以下操作:

var FooComponent = React.createClass({
    render: function() {
        var Html = (myRawHtmlMarkup);

        return (
            <Tr id={key} a={value} b={Html} />
        );
    }
});
var FooComponent=React.createClass({
render:function(){
var Html=(myRawHtmlMarkup);
返回(
);
}
});

您是如何尝试使用
危险的赛汀能HTML
?你犯了什么错误?
var Tr = React.createClass({
    componentDidMount: function(){
    var div = document.createElement('div');
    div.innerHTML = this.props.b;
    this.refs.b.appendChild( div.firstChild )
  },
    render: function() {
        return (
            <tr>
                <td className="idCol">{this.props.id}</td>
                <td className="aCol">{this.props.a}</td>
                <td className="bCol" ref="b"></td>
            </tr>
        );
    }
});

ReactDOM.render(
   <Tr id="key" a="value" b="<p><strong>Hi</strong></p>" />,
   document.querySelector('table tbody')
);
var FooComponent = React.createClass({
    render: function() {
        var Html = (myRawHtmlMarkup);

        return (
            <Tr id={key} a={value} b={Html} />
        );
    }
});