Home > Article > Web Front-end > How to remove node in react
React method to remove nodes: 1. Introduce "react-dom" through "import ReactDom from 'react-dom'"; 2. Add nodes using "ReactDom.render"; 3. Through "let state" =ReactDom.unmountComponentAtNode" to uninstall the node.
The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.
How to remove nodes in react?
react Add and uninstall nodes through react
import ReactDom from 'react-dom'
Add node:
ReactDom.render(ReactNode,挂载的dom对象)
Add content to the dom object Within, overwrite operation
Uninstall node:
let state=ReactDom.unmountComponentAtNode(挂载的dom对象) 返回是否删除成功bool值
Delete the content in the dom object
Code sample:
const div = document.createElement('div'); document.body.appendChild(div); ReactDOM.render( <组件/>, div, ); //删除 const unmountResult = ReactDOM.unmountComponentAtNode(div); if (unmountResult && div.parentNode) { div.parentNode.removeChild(div); }
Recommended learning :《react video tutorial》
The above is the detailed content of How to remove node in react. For more information, please follow other related articles on the PHP Chinese website!