
- 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 APP中引入事件
- 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 - findRenderedDOMComponentWithTag()
在React中,有一個很有用的函式叫做findRenderedDOMComponentWithTag()。這個函式就像一個特殊的偵探,在我們應用的虛擬世界中搜索名為“標籤”的特定元件。
當我們使用findRenderedDOMComponentWithTag()時,我們是在告訴我們的計算機程式在應用程式的虛擬表示(稱為DOM(文件物件模型))中找到一個特定的標籤。
該函式檢查該確切的標籤。最有趣的部分是它期望找到完全匹配項。如果有多個或沒有匹配項,它將丟擲異常。
語法
findRenderedDOMComponentWithTag( tree, tagName )
引數
tree − 它充當我們虛擬環境的地圖,顯示所有元素以及它們之間的關聯方式。
tagName − 這是我們在虛擬環境中查詢的標籤的名稱。
返回值
findRenderedDOMComponentWithTag()函式給出一個單一的結果。當我們使用此函式時,我們是在告訴我們的計算機程式在我們的應用程式的DOM中找到一個特定的HTML標籤(如<div>,<span>等)。該方法期望只識別一個與提供的標籤匹配的項。如果它檢測到多個或沒有匹配項,它將丟擲異常,表明某些內容不符合預期。
示例
示例 - 查詢標題
假設我們有一本虛擬書,我們想找到標題,它是一個標題(h1)標籤。為此,我們將使用findRenderedDOMComponentWithTag(tree, 'h1')。以下是此應用程式的程式碼:
// Import required libraries import React from 'react'; import { render } from '@testing-library/react'; import { findRenderedDOMComponentWithTag } from 'testing-library'; // App component const App = () => { return ( <div> <h1>Title of the Book</h1> </div> ); }; // Test case const { container } = render(<App1 />); const headingElement = findRenderedDOMComponentWithTag(container, 'h1'); console.log(headingElement.textContent);
輸出
Title of the Book
該程式的目標是使用findRenderedDOMComponentWithTag()函式查詢並顯示該標題。當我們要求應用程式找到標題(h1標籤)時,它應該返回短語“書名”。
示例 - 定位按鈕
讓我們假設在這個應用程式中我們有一堆按鈕。要查詢並與特定按鈕進行互動,請使用findRenderedDOMComponentWithTag(tree, 'button')。
// Import necessary libraries import React from 'react'; import { render } from '@testing-library/react'; import { findRenderedDOMComponentWithTag } from 'testing-library'; // App component const App = () => { return ( <div> <button onClick={() => console.log('Button 1 is clicked')}>Button 1</button> <button onClick={() => console.log('Button 2 is clicked')}>Button 2</button> </div> ); }; // Test case const { container } = render(<App />); const buttonElement = findRenderedDOMComponentWithTag(container, 'button'); buttonElement.click();
輸出
Button 1 is clicked
程式中有兩個按鈕,一個標記為“按鈕1”,另一個標記為“按鈕2”。這裡的目標是使用findRenderedDOMComponentWithTag()函式查詢並與這些按鈕進行互動。當我們按下按鈕時,它應該顯示一條訊息,例如“已點選按鈕1”。
示例3
讓我們建立一個應用程式,其中我們有一些影像。要找到特定影像,我們將使用findRenderedDOMComponentWithTag(tree, 'img')。
// Import required libraries import React from 'react'; import { render } from '@testing-library/react'; import { findRenderedDOMComponentWithTag } from 'testing-library'; // App component const App = () => { return ( <div> <img src="image1.jpg" alt="This is Image 1" /> <img src="image2.jpg" alt="This is Image 2" /> </div> ); }; // Test case const { container } = render(<App />); const imageElement = findRenderedDOMComponentWithTag(container, 'img'); console.log(imageElement.getAttribute('alt'));
輸出
This is Image 1
在這個應用程式中,有兩張照片,每張照片顯示不同的內容。目標是使用findRenderedDOMComponentWithTag()函式識別並顯示有關這些影像的資訊。例如,當我們找到一張圖片時,它應該告訴我們它是什麼。如果它是第一張圖片,它可以說類似於“這是圖片1”。
總結
findRenderedDOMComponentWithTag()方法充當數字瀏覽器,幫助我們在應用程式的虛擬環境中找到特定專案。它確保我們得到我們想要的東西。因此,此功能使我們的編碼體驗更加輕鬆。記住要小心使用它,並期望它只找到一件東西。如果有多於或少於一件,它會通知我們。