
- 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 - 在支出管理應用中引入事件
- 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 - Map
- 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 - Context
- ReactJS - 錯誤邊界
- ReactJS - 轉發 Refs
- ReactJS - Fragments
- ReactJS - 高階元件
- ReactJS - 與其他庫整合
- ReactJS - 效能最佳化
- ReactJS - Profiler API
- ReactJS - Portals
- ReactJS - 不使用 ES6 ECMAScript 的 React
- ReactJS - 不使用 JSX 的 React
- ReactJS - 調和
- ReactJS - Refs 和 DOM
- ReactJS - Render Props
- ReactJS - 靜態型別檢查
- ReactJS - Strict Mode
- ReactJS - Web Components
- 其他概念
- ReactJS - 日期選擇器
- ReactJS - Helmet
- ReactJS - 內聯樣式
- ReactJS - PropTypes
- ReactJS - BrowserRouter
- ReactJS - DOM
- ReactJS - 輪播圖
- ReactJS - 圖示
- ReactJS - 表單元件
- ReactJS - 參考 API
- ReactJS 有用資源
- ReactJS - 快速指南
- ReactJS - 有用資源
- ReactJS - 討論
ReactJS - findDOMNode() 方法
在使用 React 時,我們可能會遇到需要訪問元件實際瀏覽器 DOM 節點的場景。出於多種原因,這非常有用,例如直接操作 DOM 和整合需要訪問特定元素的第三方庫。我們可以透過使用 react-dom 包提供的 findDOMNode 方法來實現。
什麼是 findDOMNode?
findDOMNode 是一個 react-dom 庫方法,允許我們定位與 React 類元件物件關聯的瀏覽器 DOM 節點。換句話說,它幫助我們識別 React 元件實際渲染的 HTML 元素。
語法
import { findDOMNode } from 'react-dom'; const dn = findDOMNode(componentInstance);
引數
componentInstance − 它是 React 類元件的例項,domNode 將儲存相應的 DOM 元素。
返回值
findDOMNode 函式返回最接近提供的 componentInstance 的第一個瀏覽器 DOM 節點。但是,有一些因素需要考慮:
如果元件渲染為 null 或 false,findDOMNode 將返回 null。
findDOMNode 返回具有元件渲染為字串值的文字 DOM 節點。
此外,如果元件提供具有多個子元素的陣列或 Fragment,findDOMNode 將返回第一個非空子元素。
示例
示例 - 選擇輸入應用
當輸入文字在元件中掛載時,我們可以使用 findDOMNode 函式自動選擇它。以下是我們可以更新應用以包含 findDOMNode 函式的方法:
首先,我們需要建立一個 React 應用,然後建立一個名為 SelectingInput.js 的檔案,在這個檔案中,我們將有一個元件來使用 findDOMNode 函式在輸入欄位掛載後自動選擇其中的文字。
現在,當我們單擊“顯示輸入”按鈕時,App 元件將使用 SelectingInput 元件。SelectingInput 元件內的輸入欄位將在掛載後自動選擇其文字。
SelectingInput.js
import React, { Component } from 'react'; import { findDOMNode } from 'react-dom'; class SelectingInput extends Component { componentDidMount() { const input = findDOMNode(this.inputRef); input.select(); } render() { return ( <div> <input defaultValue="Hello" ref={(input) => (this.inputRef = input)} /> </div> ); } } export default SelectingInput;
App.js
import React, { useState } from 'react'; import SelectingInput from './SelectingInput'; function App() { const [show, setShow] = useState(false); return ( <> <button onClick={() => setShow(true)}>Show the input</button> <hr /> {show && <SelectingInput />} </> ); } export default App;
輸出

此示例演示瞭如何在 React 應用中使用 findDOMNode 函式在元件掛載時與 DOM 互動。
示例 - 渲染字串
在這個例子中,我們將建立一個基本的 React 應用程式,它包含兩個元件:App 和 MyComponent。它是一個擴充套件 React 提供的 Component 類的類元件。render 方法返回一個帶有文字“This is a simple string.”的 <h1> HTML 元素。App 元件是我們的應用程式的主要元件。在 componentDidMount 生命週期方法中,它使用 findDOMNode 函式獲取實際的 DOM 節點。然後將獲得的 DOM 節點記錄到控制檯中。在 App 元件的 render 方法中,MyComponent 的例項在 <div> 內渲染。
import React, { Component } from 'react'; import { findDOMNode } from 'react-dom'; class MyComponent extends Component { render() { return <h1>This is a simple string</h1>; // Rendering to string } } class App extends Component { componentDidMount() { const domNode = findDOMNode(this.myComponentInstance); console.log(domNode); } render() { return ( <div> <MyComponent ref={(component) => (this.myComponentInstance = component)} /> </div> ); } } export default App;
輸出

執行此應用程式時,它將呈現一個簡單的 <h1> 元素,其中包含文字“This is a simple string.”。然後,App 元件的 componentDidMount 生命週期方法將 MyComponent 的關聯 DOM 節點記錄到控制檯中。
示例 - 計數器應用
這是一個使用 findDOMNode 函式的另一個簡單的 React 應用。我們將有一個 Counter 元件,它是一個維護計數狀態的類元件。它有一個按鈕,點選時可以增加計數。componentDidMount 方法將相關的 DOM 節點記錄到控制檯中。我們的主元件是 App,它在 <div> 內渲染 Counter 元件,並使用 ref 屬性獲取對 Counter 例項的引用。
import React, { Component } from 'react'; import { findDOMNode } from 'react-dom'; class Counter extends Component { constructor() { super(); this.state = { count: 0, }; } handleIncrement = () => { this.setState((prevState) => ({ count: prevState.count + 1 })); }; componentDidMount() { const domNode = findDOMNode(this.counterInstance); console.log(domNode); } render() { return ( <div> <h1>Counter: {this.state.count}</h1> <button onClick={this.handleIncrement}>Increment</button> </div> ); } } class App extends Component { render() { return ( <div> <Counter ref={(component) => (this.counterInstance = component)} /> </div> ); } } export default App;
輸出

因此,當我們點選“遞增”按鈕時,計數會增加。在控制檯中,我們將看到元件掛載時記錄的 Counter 元件的關聯 DOM 節點。
總結
findDOMNode 是一個用於彌合 React 虛擬 DOM 和實際瀏覽器 DOM 之間的差距的有用工具。但是我們應該謹慎使用它,並在可能的情況下尋找其他方法,因為直接在我們的 React 應用程式中訪問 DOM 會導致意外行為。