Home  >  Article  >  Web Front-end  >  How to remove node in react

How to remove node in react

藏色散人
藏色散人Original
2023-01-18 16:12:091314browse

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.

How to remove node in react

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn