gpt4 book ai didi

reactjs - react : How to add HTMLElement?

转载 作者:行者123 更新时间:2023-12-03 14:13:23 25 4
gpt4 key购买 nike

我正在尝试在 React 组件中包装一个返回 HTMLElement 对象的外部库。

现在,我只是定义一个引用并附加对象,如下所示:

class MyComponent extends React.Component {
constructor(props) {
super(props)
this.externalLibrary = new ExternalLibrary()
}
componentDidMount() {
this.externalLibrary()
.then(contents => this.div.appendChild(contents))
}
render() {
<div ref={div => this.div = div} />
}
}

但是 AFAIU 这意味着 DOM 的这一部分将不会由 React 管理。添加 HTMLElement 的正确方法是什么?

最佳答案

您需要使用dangerouslySetInnerHTML

  componentDidMount() {
let self= this;
this.externalLibrary()
.then(contents => self.setState({contents}))
}
render() {
<div dangerouslySetInnerHTML={{__html: this.state.contents}} />
}

关于reactjs - react : How to add HTMLElement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49297334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com