简体   繁体   English

尽管DOM已修改,但仍强制重新渲染React组件

[英]Force a React component to re-render despite a modified DOM

In addition of React, I am using an external library called Prism.js to do syntax highlighting. 除了React,我还使用一个名为Prism.js的外部库来突出显示语法。 I've a React component that generates some example code. 我有一个React组件,可以生成一些示例代码。 First render is fine. 第一次渲染很好。 The component renders with its initial props, then I call Prism.highlightElement(); 该组件使用其初始道具进行渲染,然后调用Prism.highlightElement(); in componentDidUpdate() that does the syntax highlighting fine as well. componentDidUpdate()中,语法突出显示也很好。 However, we've inputs in the React parent component that changes the content of the code needed to be generated. 但是,我们在React父组件中输入了内容,这些内容更改了需要生成的代码的内容。 When changes are made, the generated highlighted code stay the same. 进行更改后,生成的突出显示的代码保持不变。 Other parts of the child component that are not been syntax highlighted thought Prism.js do update fine. 子组件的其他未语法突出显示的部分认为Prism.js确实可以很好地更新。 So, it seems to be an issue with React not re-rendering modified DOM. 因此,React不重新呈现修改后的DOM似乎是一个问题。

Here is a simplified version of the child component: 这是子组件的简化版本:

class CodeExample extends React.Component {

  constructor(props) {
    super(props);
  }

  componentWillReceiveProps(newProps) {
    this.forceUpdate();
    if(document.getElementById('integrationExample')) {
      Prism.highlightElement(document.getElementById('integrationExample'));
    }
  }

  componentDidUpdate() {
    if(document.getElementById('integrationExample')) {
      Prism.highlightElement(document.getElementById('integrationExample'));
    }
  }


  render() {
    return (
      <div>
        <pre>
          <code className="language-bash" id="integrationExample">$ curl {this.props.resultsUrl}
          </code>
        </pre>
      </div>
    )
  }

}

I've tried to move to state from props, componentWillReceiveProps() , componentDidUpdate() , this.forceUpdate(); 我已经尝试从props, componentWillReceiveProps()componentDidUpdate()this.forceUpdate();转移到状态this.forceUpdate(); without success. 没有成功。 The highlighted code stays as if the props didn't change. 高亮显示的代码保持不变,好像道具没有变化。 Removing Prism.js fixed the issue, and the component updates fine. 删除Prism.js可解决此问题,并且组件更新正常。

Any ideas to force the code sample to re-render while keeping Prism.js? 有什么想法可以在保留Prism.js的同时强制重新渲染代码示例?

Haven't tried this out, but I would try this with componentDidMount and componentDidUpdate , and see how it goes using a ref instead of document.getElementById . 还没有尝试过,但是我将使用componentDidMountcomponentDidUpdate尝试一下,看看如何使用ref而不是document.getElementById

class CodeExample extends React.Component {
  highlight(){
    Prism.highlightElement(this.el);
  }
  componentDidMount(){
    this.highlight();
  }
  componentDidUpdate(){
    this.highlight();
  }
  render() {
    return (
      <div>
        <pre>
          <code className="language-bash" ref={el => this.el = el}>$ curl {this.props.resultsUrl}
          </code>
        </pre>
      </div>
    )
  }
}

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

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