Home > Web Front-end > Front-end Q&A > How to remove node in react

How to remove node in react

藏色散人
Release: 2023-01-18 16:12:09
Original
1363 people have browsed it

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'
Copy after login

Add node:

ReactDom.render(ReactNode,挂载的dom对象)
Copy after login

Add content to the dom object Within, overwrite operation

Uninstall node:

let state=ReactDom.unmountComponentAtNode(挂载的dom对象)    返回是否删除成功bool值
Copy after login

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);
  }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template