gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:10:15 24 4
gpt4 key购买 nike

我在使用reactDom创建和删除组件时遇到问题

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

// 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.

最佳答案

您应该根据 bool 值来渲染它,即

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

并在 closeComponent 中

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

关于javascript - 如何在React中创建和删除组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57403008/

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