如何在 React 中创建和删除组件?

How to create and delete a component in react?

我在创建和删除与 reactDom 反应的组件时遇到问题

我正试图从 class 中销毁弹出组件,但它给了我一个警告

// file: successfull.js
mostrarPopUp(enterprise) {
        let contenedor = document.getElementById('popup');
        ReactDOM.render(<Popup empresa={enterprise} />, contenedor);
}

// file: popup.js
closeComponent() {
        console.log("Destruir componente");
        let element = document.getElementById("contenedorpop");
        let container = ReactDOM.findDOMNode(element);
        ReactDOM.unmountComponentAtNode(container);
}

我有下一个警告

Warning: unmountComponentAtNode(): The node you're attempting to unmount was rendered by React and is not a top-level container. You may have accidentally passed in a React root node instead of its container.

您应该根据布尔值渲染它,即

 { this.state.shouldIRender === true ?  <Popup empresa={enterprise} /> : null }

并在 closeComponent

closeComponent() {
   this.setState({
    shouldIRender: false
   })
}