- 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 - 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 - 片段
- 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 - findRenderedComponentWithType()
React.js 中有一個名為 findRenderedComponentWithType() 的函式。此函式類似於另一個名為 scryRenderedComponentsWithType() 的函式,但其工作方式不同。findRenderedComponentWithType() 背後的基本思想是在渲染樹中定位並返回給定的元件。
findRenderedComponentWithType() 的目標是在渲染樹(React 元件的結構)中搜索給定的元件。與其他幾個函式相比,findRenderedComponentWithType() 只需要一個匹配項。如果有多個匹配項或根本沒有匹配項,則會丟擲異常。
語法
findRenderedComponentWithType(tree, componentClass)
引數
tree − 這是我們要搜尋的 React 元件的渲染樹。
componentClass − 這是我們想要的元件型別,由其類給出。
返回值
findRenderedComponentWithType() 返回渲染樹中與給定類型別匹配的單個 React 元件。如果只有一個匹配項,它將返回該元件。如果沒有匹配項或多個匹配項,它將丟擲異常,指示預期匹配數存在問題。
示例
示例 - 計數器應用和測試
這是一個基本的計數器應用,其中我們將有一個按鈕用於遞增計數器。然後,我們將為此元件建立測試以測試其功能。讓我們看看應用和測試的程式碼:
CounterApp.js
import React, { useState } from 'react';
const CounterApp = () => {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
return (
<div>
<h1>Counter App</h1>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
};
export default CounterApp;
CounterApp.test.js
import React from 'react';
import { render } from '@testing-library/react';
import { findRenderedComponentWithType } from 'react-dom/test-utils';
import CounterApp from './CounterApp';
test('finds and interacts with the "Increment" button', () => {
const { container } = render(<CounterApp />);
const incrementButton = findRenderedComponentWithType(container, HTMLButtonElement);
// The testing logic goes here
expect(incrementButton.textContent).toBe('Increment');
// Additional assertions
});
輸出
示例 - 電子郵件驗證應用和測試
現在,我們將建立一個小型應用程式,該應用程式將驗證使用者的電子郵件地址,並使用 findRenderedComponentWithType() 方法建立測試程式碼:
FormValidationApp.js
import React, { useState } from 'react';
const FormValidationApp = () => {
const [email, setEmail] = useState('');
const [validEmail, setValidEmail] = useState(false);
const handleEmailChange = (e) => {
const newEmail = e.target.value;
setEmail(newEmail);
setValidEmail(validateEmail(newEmail));
};
const validateEmail = (input) => {
// Simple email validation logic
return /\S+@\S+\.\S+/.test(input);
};
return (
<div>
<h1>Form Validation App</h1>
<label>Email:</label>
<input type="text" value={email} onChange={handleEmailChange} />
{validEmail ? <p>Email is valid!</p> : <p>Email is not valid.</p>}
</div>
);
};
export default FormValidationApp;
FormValidationApp.test.js
import React from 'react';
import { render } from '@testing-library/react';
import { findRenderedComponentWithType } from 'react-dom/test-utils';
import FormValidationApp from './FormValidationApp';
test('finds and validates the email input', () => {
const { container } = render(<FormValidationApp />);
const emailInput = findRenderedComponentWithType(container, HTMLInputElement);
//testing logic
expect(emailInput.type).toBe('text');
});
輸出
示例 - 切換開關應用和測試
在這個應用中,我們將使用切換功能,其中具有開啟和關閉功能,然後我們將使用 findRenderedComponentWithType() 為此功能建立測試。此應用和測試程式碼如下:
ToggleSwitchApp.js
import React, { useState } from 'react';
const ToggleSwitchApp = () => {
const [isOn, setIsOn] = useState(false);
const toggleSwitch = () => {
setIsOn(!isOn);
};
return (
<div>
<h1>Toggle Switch App</h1>
<label>
<input type="checkbox" checked={isOn} onChange={toggleSwitch} />
{isOn ? 'ON' : 'OFF'}
</label>
</div>
);
};
export default ToggleSwitchApp;
ToggleSwitchApp.test.js
import React from 'react';
import { render } from '@testing-library/react';
import { findRenderedComponentWithType } from 'react-dom/test-utils';
import ToggleSwitchApp from './ToggleSwitchApp';
test('finds and interacts with the toggle switch', () => {
const { container } = render(<ToggleSwitchApp />);
const toggleSwitch = findRenderedComponentWithType(container, HTMLInputElement);
// testing logic
expect(toggleSwitch.type).toBe('checkbox');
});
輸出
總結
在本教程中,我們學習了 findRenderedComponentWithType(),這是一個 React.js 函式,有助於在渲染樹中定位特定元件。我們分析了它的引數,然後利用這些知識建立了三個不同的 React.js 應用。我們不僅理解了這個概念,而且還將其付諸實踐,使我們的學習更加實踐化。