简体   繁体   English

反应如何将innerHtml添加到动态创建的子元素

[英]React how to add innerHtml to a dynamically created child element

Ive got a parent component "Dialog" that looks like the below. Ive的父组件“对话框”如下所示。 It pulls in another child component "DialogBody" that requires a child react node. 它引入另一个需要子反应节点的子组件“ DialogBody”。 How can i set the innerHTML of the dynamically created "child1" div versus the parent 'getBodyHtml' (or the 'this.props.body') ? 我如何设置动态创建的“ child1” div的innerHTML与父“ getBodyHtml”(或“ this.props.body”)?

class Dialog extends Component {
getBodyHtml () {
    return {
        __html: this.props.body,
    };
}
render () {

var child1 = React.createElement('div', null);

//how to set innerhtml here ??
child1.dangerouslySetInnerHTML({this.getBodyHtml()});

    return (
            {/* children is a required prop of react component */}
            <DialogBody children={child1} />
            </Dialog>
    );

You could remove the compulsory children prop and set dangerouslySetInnerHTML like this: 您可以删除必需的children道具,并像这样dangerouslySetInnerHTML设置dangerouslySetInnerHTML

render() {
  return (
    <DialogBody dangerouslySetInnerHTML={this.getBodyHtml()}/></Dialog>
  )
}

For more info, refer to the docs here 有关更多信息,请参考此处的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM