
- ReactJS 教程
- ReactJS - 首頁
- ReactJS - 簡介
- ReactJS - 路線圖
- ReactJS - 安裝
- ReactJS - 特性
- ReactJS - 優點與缺點
- ReactJS - 架構
- ReactJS - 建立 React 應用
- ReactJS - JSX
- ReactJS - 元件
- ReactJS - 巢狀元件
- ReactJS - 使用新建立的元件
- ReactJS - 元件集合
- ReactJS - 樣式
- ReactJS - 屬性 (props)
- ReactJS - 使用屬性建立元件
- ReactJS - props 驗證
- ReactJS - 建構函式
- ReactJS - 元件生命週期
- ReactJS - 事件管理
- ReactJS - 建立一個事件感知元件
- ReactJS - 在 Expense Manager 應用中引入事件
- ReactJS - 狀態管理
- ReactJS - 狀態管理 API
- ReactJS - 無狀態元件
- ReactJS - 使用 React Hooks 進行狀態管理
- ReactJS - 使用 React Hooks 進行元件生命週期管理
- ReactJS - 佈局元件
- ReactJS - 分頁
- ReactJS - Material UI
- ReactJS - Http 客戶端程式設計
- ReactJS - 表單程式設計
- ReactJS - 受控元件
- ReactJS - 非受控元件
- ReactJS - Formik
- ReactJS - 條件渲染
- ReactJS - 列表
- ReactJS - Keys
- ReactJS - 路由
- ReactJS - Redux
- ReactJS - 動畫
- ReactJS - Bootstrap
- ReactJS - 地圖
- ReactJS - 表格
- ReactJS - 使用 Flux 管理狀態
- ReactJS - 測試
- ReactJS - CLI 命令
- ReactJS - 構建和部署
- ReactJS - 示例
- Hooks
- ReactJS - Hooks 簡介
- ReactJS - 使用 useState
- ReactJS - 使用 useEffect
- ReactJS - 使用 useContext
- ReactJS - 使用 useRef
- ReactJS - 使用 useReducer
- ReactJS - 使用 useCallback
- ReactJS - 使用 useMemo
- ReactJS - 自定義 Hooks
- ReactJS 高階
- ReactJS - 可訪問性
- ReactJS - 程式碼分割
- ReactJS - 上下文
- ReactJS - 錯誤邊界
- ReactJS - 轉發 Refs
- ReactJS - 片段
- ReactJS - 高階元件
- ReactJS - 與其他庫整合
- ReactJS - 最佳化效能
- ReactJS - Profiler API
- ReactJS - 埠
- ReactJS - 無 ES6/ECMAScript 的 React
- ReactJS - 無 JSX 的 React
- ReactJS - 調和
- ReactJS - Refs 和 DOM
- ReactJS - 渲染 Props
- ReactJS - 靜態型別檢查
- ReactJS - 嚴格模式
- ReactJS - Web Components
- 其他概念
- ReactJS - 日期選擇器
- ReactJS - Helmet
- ReactJS - 內聯樣式
- ReactJS - PropTypes
- ReactJS - BrowserRouter
- ReactJS - DOM
- ReactJS - 輪播圖
- ReactJS - 圖示
- ReactJS - 表單元件
- ReactJS - 參考 API
- ReactJS 有用資源
- ReactJS - 快速指南
- ReactJS - 有用資源
- ReactJS - 討論
ReactJS - 元件生命週期
在 React 中,元件的生命週期表示元件在其存在期間的不同階段。React 提供回撥函式,以便在 React 生命週期每個階段都附加功能。在本章中,讓我們學習 React 元件的生命週期(以及相關的 API)。
生命週期 API
每個 React 元件都有三個不同的階段。
掛載 − 掛載表示在給定的 DOM 節點中渲染 React 元件。
更新 − 更新表示在狀態更改/更新期間在給定的 DOM 節點中重新渲染 React 元件。
解除安裝 − 解除安裝表示刪除 React 元件。
React 提供了一系列生命週期事件(或回撥 API),用於附加功能,這些功能將在元件的不同階段執行。生命週期的視覺化以及生命週期事件(API)呼叫的順序如下所示。

constructor() − 在 React 元件的初始構造階段呼叫。用於設定元件的初始狀態和屬性。
render() − 在元件構造完成後呼叫。它在虛擬 DOM 例項中渲染元件。這在 DOM 樹中指定為元件的掛載。
componentDidMount() − 在元件在 DOM 樹中初始掛載後呼叫。這是呼叫 API 端點和執行網路請求的好地方。在我們的時鐘元件中,可以在這裡設定 setInterval 函式以每秒更新狀態(當前日期和時間)。
componentDidMount() { this.timeFn = setInterval( () => this.setTime(), 1000); }
componentDidUpdate() − 類似於 ComponentDidMount(),但在更新階段呼叫。在此階段可以執行網路請求,但僅當元件的當前屬性和先前屬性之間存在差異時。
API 的簽名如下所示:
componentDidUpdate(prevProps, prevState, snapshot)
prevProps − 元件的先前屬性。
prevState − 元件的先前狀態。
snapshot − 當前渲染的內容。
componentWillUnmount() − 在元件從 DOM 中解除安裝後呼叫。這是清理物件的好地方。在我們的時鐘示例中,我們可以在此階段停止更新日期和時間。
componentDidMount() { this.timeFn = setInterval( () => this.setTime(), 1000); }
shouldComponentUpdate() − 在更新階段呼叫。用於指定元件是否應該更新。如果它返回 false,則更新將不會發生。
簽名如下所示:
shouldComponentUpdate(nextProps, nextState)
nextProps − 元件即將到來的屬性
nextState − 元件即將到來的狀態
getDerivedStateFromProps − 在初始和更新階段以及 render() 方法之前呼叫。它返回新的狀態物件。在屬性更改導致狀態更改的情況下,它很少使用。它主要用於動畫上下文中,其中需要元件的各種狀態才能執行平滑動畫。
API 的簽名如下所示:
static getDerivedStateFromProps(props, state)
props − 元件的當前屬性
state − 元件的當前狀態
這是一個靜態方法,無法訪問 this 物件。
getSnapshotBeforeUpdate − 在渲染的內容提交到 DOM 樹之前呼叫。它主要用於獲取有關新內容的一些資訊。此方法返回的資料將傳遞給 ComponentDidUpdate() 方法。例如,它用於維護新生成內容中的使用者滾動位置。它返回使用者滾動位置。此滾動位置由 componentDidUpdate() 用於設定實際 DOM 中輸出的滾動位置。
API 的簽名如下所示:
getSnapshotBeforeUpdate(prevProps, prevState)
prevProps − 元件的先前屬性。
prevState − 元件的先前狀態。
生命週期 API 的工作示例
讓我們在我們的 react-clock-app 應用程式中使用生命週期 API。
步驟 1 − 在您喜歡的編輯器中開啟 react-clock-hook-app。
開啟 src/components/Clock.js 檔案並開始編輯。
步驟 2 − 從建構函式中刪除 setInterval() 方法。
constructor(props) { super(props); this.state = { date: new Date() } }
步驟 3 − 新增 componentDidMount() 方法並呼叫 setInterval() 以每秒更新日期和時間。此外,儲存引用以稍後停止更新日期和時間。
componentDidMount() { this.setTimeRef = setInterval(() => this.setTime(), 1000); }
新增 componentWillUnmount() 方法並呼叫 clearInterval() 以停止日期和時間更新呼叫。
componentWillUnmount() { clearInterval(this.setTimeRef) }
現在,我們已經更新了 Clock 元件,並且元件的完整原始碼如下所示:
import React from 'react'; class Clock extends React.Component { constructor(props) { super(props); this.state = { date: new Date() } } componentDidMount() { this.setTimeRef = setInterval(() => this.setTime(), 1000); } componentWillUnmount() { clearInterval(this.setTimeRef) } setTime() { this.setState((state, props) => { console.log(state.date); return { date: new Date() } }) } render() { return ( <div> <p>The current time is {this.state.date.toString()}</p> </div> ); } } export default Clock;
接下來,開啟 index.js 並使用 setTimeout 在 5 秒後從 DOM 中刪除時鐘。
import React from 'react'; import ReactDOM from 'react-dom'; import Clock from './components/Clock'; ReactDOM.render( <React.StrictMode> <Clock /> </React.StrictMode>, document.getElementById('root') ); setTimeout(() => { ReactDOM.render( <React.StrictMode> <div><p>Clock is removed from the DOM.</p></div> </React.StrictMode>, document.getElementById('root') ); }, 5000);
使用 npm 命令提供應用程式。
npm start
開啟瀏覽器並在位址列中輸入 https://:3000 並按 Enter 鍵。
時鐘將顯示 5 秒,然後將從 DOM 中刪除。透過檢查控制檯日誌,我們可以發現清理程式碼已正確執行。

在 Expense Manager 應用中的生命週期 API
讓我們在 Expense Manager 中新增生命週期 API,並在每次呼叫 API 時記錄它。這將深入瞭解元件的生命週期。
步驟 1 − 在您喜歡的編輯器中開啟 expense-manager 應用程式。
接下來,使用以下方法更新 ExpenseEntryItemList 元件。
componentDidMount() { console.log("ExpenseEntryItemList :: Initialize :: componentDidMount :: Component mounted"); } shouldComponentUpdate(nextProps, nextState) { console.log("ExpenseEntryItemList :: Update :: shouldComponentUpdate invoked :: Before update"); return true; } static getDerivedStateFromProps(props, state) { console.log("ExpenseEntryItemList :: Initialize / Update :: getDerivedStateFromProps :: Before update"); return null; } getSnapshotBeforeUpdate(prevProps, prevState) { console.log("ExpenseEntryItemList :: Update :: getSnapshotBeforeUpdate :: Before update"); return null; } componentDidUpdate(prevProps, prevState, snapshot) { console.log("ExpenseEntryItemList :: Update :: componentDidUpdate :: Component updated"); } componentWillUnmount() { console.log("ExpenseEntryItemList :: Remove :: componentWillUnmount :: Component unmounted"); }
步驟 2 − 使用 npm 命令提供應用程式。
npm start
開啟瀏覽器並在位址列中輸入 https://:3000 並按 Enter 鍵。
接下來,檢查控制檯日誌。它將在初始化階段顯示生命週期 API,如下所示。
ExpenseEntryItemList :: Initialize / Update :: getDerivedStateFromProps :: Before update ExpenseEntryItemList :: Initialize :: componentDidMount :: Component mounted
刪除一個專案,然後檢查控制檯日誌。它將在更新階段顯示生命週期 API,如下所示。
ExpenseEntryItemList :: Initialize / Update :: getDerivedStateFromProps :: Before update ExpenseEntryItemList.js:109 ExpenseEntryItemList :: Update :: shouldComponentUpdate invoked :: Before update ExpenseEntryItemList.js:121 ExpenseEntryItemList :: Update :: getSnapshotBeforeUpdate :: Before update ExpenseEntryItemList.js:127 ExpenseEntryItemList :: Update :: componentDidUpdate :: Component updated
最後,刪除所有生命週期 API,因為它可能會影響應用程式效能。僅當情況需要時才應使用生命週期 API。