React.js 庫就是將應用程序拆分為多個組件。每個組件都有自己的生命周期。 React 為我們提供了一些內置方法,我們可以在組件的 life-cycle 中的特定階段覆蓋這些方法。
在基於類的組件中, unmountComponentAtNode() 方法從 DOM 中移除一個掛載的 React 組件。
創建React應用程序:
步驟 1:使用以下命令創建 React 應用程序。
npx create-react-app foldername
第 2 步:創建項目文件夾(即文件夾名稱)後,使用以下命令移至該文件夾。
cd foldername
項目結構:如下圖所示。
例:現在在 App.js 文件中寫下以下代碼。在這裏,App 是我們編寫代碼的默認組件。
App.js
import React from 'react'
import ReactDOM from 'react-dom';
// Exporting your App Component
export default function App() {
// Function to unmount root component
function unm() {
ReactDOM.unmountComponentAtNode(document.getElementById("root"));
}
return (
<div>
<h1>GeeksforGeeks</h1>
<div>Hi Geek, this is the rendered content</div>
<button onClick={unm}>Unmount</button>
</div>
);
}
運行應用程序的步驟:從項目的根目錄中使用以下命令運行應用程序:
npm start
輸出:現在打開瀏覽器並轉到http://localhost:3000 /,您將看到以下輸出:
參考:https://reactjs.org/docs/react-dom.html#unmountcomponentatnode
相關用法
- ReactJS shouldComponentUpdate()用法及代碼示例
- ReactJS componentDidMount()用法及代碼示例
- ReactJS getSnapshotBeforeUpdate()用法及代碼示例
- ReactJS bind()用法及代碼示例
- ReactJS componentDidUpdate()用法及代碼示例
- ReactJS getDerivedStateFromError()用法及代碼示例
- ReactJS componentDidCatch()用法及代碼示例
- ReactJS forceUpdate()用法及代碼示例
- ReactJS findDOMNode()用法及代碼示例
- ReactJS isElementOfType()用法及代碼示例
- ReactJS isCompositeComponent()用法及代碼示例
- ReactJS isElement()用法及代碼示例
- ReactJS isCompositeComponentWithType()用法及代碼示例
- ReactJS isDOMComponent()用法及代碼示例
- ReactJS TestRenderer.unmount()用法及代碼示例
- ReactJS TestRenderer.create()用法及代碼示例
- ReactJS TestRenderer.toJSON()用法及代碼示例
- ReactJS TestRenderer.toTree()用法及代碼示例
注:本文由純淨天空篩選整理自dheerchanana08大神的英文原創作品 ReactJS unmountComponentAtNode() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。