2
我是React新手,不确定处理以下情况的正确方法:如何处理React中的第三方dom操作?
我做了一个呈现代码并使用Highlight.js突出显示语法的组件。
它的工作,但内容更新时坏了。
class CodeBox extends React.Component {
componentDidMount() {
this.highlight();
}
componentDidUpdate() {
this.highlight();
}
highlight() {
hljs.highlightBlock(this.elem);
}
render() {
return (
<pre><code ref={(elem) => { this.elem = elem }}>{this.props.code}</code></pre>
);
}
}
我的理解是,作出反应处理code
节点,当Highlight.js与它篡改不喜欢...所以我使出这样的:
class CodeBox extends React.Component {
componentDidMount() {
this.highlight();
}
componentDidUpdate() {
this.highlight();
}
highlight() {
this.elem.innerHTML = "";
let c = document.createElement("code");
c.innerHTML = this.props.code;
this.elem.appendChild(c);
hljs.highlightBlock(c);
}
render() {
return (
<pre ref={(elem) => { this.elem = elem }}></pre>
);
}
}
其中一期工程,但现在我觉得我正在使用React错误。
有没有办法做到这一点,不涉及直接操纵dom?
谢谢,那就是我一直在寻找的! –