你如何在reactjs中重新安装组件?

时间:2014-06-03 12:34:31

标签: javascript reactjs

我有一个需要重新安装的组件。我需要销毁所有内容并使其代码重新执行以再次出现在我的页面上。

我尝试过的: https://groups.google.com/forum/#!topic/reactjs/8JgClU9jol0我把key =“1”然后用javascript将其密钥更改为其他内容,它没有重新安装

Unmounting React.js node这不起作用,因为我的组件已经渲染。

请帮忙,我该怎么做?

1 个答案:

答案 0 :(得分:0)

这里是一个例子。

// t is the thread that is doing the await() call above
final Thread t = Thread.currentThread();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
      t.interrupt();
      try {
          t.join();
      } catch (final InterruptedException interruptedException) {
        Thread.currentThread().interrupt();
      }
 }));
class Child extends React.Component {
  componentDidMount = () => {
    console.log("mounted");
  }
  
  render(){
    return(
      <div>This is child</div>
    );
  }
}

class Application extends React.Component {
  state = {
    show: true,
  };
  
  render(){
    return(
      <div>
        <button onClick={() => this.setState({show: !this.state.show})}>Click here</button>
        {this.state.show && <Child />}
      </div>
    );
  }
}

ReactDOM.render(<Application />, document.getElementById('app'));