
- 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 - 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 - 事件物件
在 React 中,當我們為按鈕點選等事件構建事件處理程式時,我們會得到一個稱為 React 事件物件(或合成事件)的特殊物件。此物件充當我們的程式碼和瀏覽器事件之間的橋樑。它減少了 Web 瀏覽器之間的差異,並提供了一些重要資訊。
如何使用它?
讓我們使用一個簡單的示例來分解它 -
假設我們在我們的 React 應用中有一個按鈕,我們希望在單擊它時執行某些操作。因此,我們將定義一個 onClick 處理程式函式,如下所示 -
示例
<button onClick={e => { console.log(e); // The event object }} />
在此程式碼中,“e”是我們的 React 事件物件,它提供有關按鈕點選的資訊。
發現事件物件可以做什麼!
讓我們瀏覽一下 React 事件物件實現的一些標準事件屬性 -
序號 | 屬性和描述 |
---|---|
1 | e.bubbles 這告訴我們事件是否在網頁上的元素之間“冒泡”。例如,如果我們有一個在 div 內的按鈕,則點選事件可能會從按鈕冒泡到 div。它是一個布林值(true 或 false)。 |
2 | e.cancelable 它表明我們可以停止事件的預設行為。例如,我們可以阻止在單擊按鈕時提交表單。它也是布林型別。 |
3 | e.currentTarget 它指向在我們的 React 元件中附加事件處理程式的元素。在本例中,它是對按鈕的引用。 |
4 | e.defaultPrevented 告訴我們是否有人使用 preventDefault() 來停止事件的預設操作。一個布林值。 |
5 | e.eventPhase 它是一個數字,告訴我們事件處於哪個階段(如捕獲、目標或冒泡)。 |
6 | e.isTrusted 顯示事件是否由使用者啟動。如果為 true,則為真正的使用者操作;如果為 false,則可能是由程式碼觸發的。 |
7 | e.target 這指向實際發生事件的元素。它可能是按鈕。 |
8 | e.timeStamp 它提供事件發生的時間。 |
9 | e.nativeEvent 我們可以使用它來訪問底層的瀏覽器事件。 |
示例 - 使用事件物件
讓我們建立一個簡單的應用程式來顯示事件物件屬性的工作原理。因此,在這個應用程式中,我們建立一個名為 EventHandlingExample.js 的元件,然後我們將在 App.js 元件中使用此元件來檢視其工作原理。
EventHandlingExample.js
import React from 'react'; class EventHandlingExample extends React.Component { handleClick = (e) => { // Logging event properties console.log('Bubbles:', e.bubbles); console.log('Cancelable:', e.cancelable); console.log('Current Target:', e.currentTarget); console.log('Default Prevented:', e.defaultPrevented); console.log('Event Phase:', e.eventPhase); console.log('Is Trusted:', e.isTrusted); console.log('Target:', e.target); console.log('Time Stamp:', e.timeStamp); // Prevent the default behavior e.preventDefault(); } render() { return ( <div> <button onClick={this.handleClick}>Click Me</button> </div> ); } } export default EventHandlingExample;
App.js
import React from 'react'; import './App.css'; import EventHandlingExample from './EventHandlingExample'; function App() { return ( <div className="App"> <h1>React Event Object Example</h1> <EventHandlingExample /> </div> ); } export default App;
輸出

在上面的示例程式碼中,當我們單擊“單擊我”按鈕時,事件屬性將記錄在控制檯中。e.preventDefault() 行顯示瞭如何阻止按鈕執行其預設操作。
標準事件方法
讓我們瀏覽一下 React 事件物件實現的一些標準事件方法 -
序號 | 方法和描述 |
---|---|
1 | e.preventDefault() 此方法可以阻止事件的預設行為。例如,您可以使用它來阻止表單提交。 |
2 | e.stopPropagation() 這會阻止事件進一步向上遍歷 React 元件樹。這有助於防止其他事件處理程式執行。 |
3 | e.isDefaultPrevented() 檢視是否呼叫了 preventDefault()。返回布林值。 |
4 | e.isPropagationStopped() 檢視是否呼叫了 stopPropagation()。返回布林值。 |
示例 - 使用事件方法
讓我們建立一個基本的 React 應用,該應用使用事件物件方法來阻止預設行為並在單擊按鈕時停止事件傳播。確保專案包含 React 和 ReactDOM。建立一個名為 EventMethodsExample 的新 React 元件,該元件顯示事件物件方法的使用 -
EventMethodsExample.js
import React from 'react'; class EventMethodsExample extends React.Component { handleClick = (e) => { // Prevent the default behavior of the button e.preventDefault(); // Stop the event from propagating e.stopPropagation(); } render() { return ( <div> <button onClick={this.handleClick}>Click Me</button> <p>Clicking the button will prevent the default behavior and stop event propagation.</p> </div> ); } } export default EventMethodsExample;
App.js
import React from 'react'; import './App.css'; import EventMethodsExample from './EventMethodsExample'; function App() { return ( <div className="App"> <h1>React Event Object Methods Example</h1> <EventMethodsExample /> </div> ); } export default App;
輸出

當我們執行 React 應用程式後單擊“單擊我”按鈕時,它會阻止預設行為(如表單提交)並阻止事件進一步向上傳播到 React 元件樹。
此示例演示如何使用事件物件的 preventDefault() 和 stopPropagation() 方法來管理事件行為。
示例 - 使用事件物件和方法
讓我們再建立一個示例應用程式來演示 React 事件物件的使用。在此示例中,我們將建立一個簡單的 React 應用程式,該應用程式響應按鍵事件。
KeyPressExample.js
import React from 'react'; class KeyPressExample extends React.Component { handleKeyPress = (e) => { // Log the key that was pressed console.log('Pressed Key:', e.key); } render() { return ( <div> <h2>React Key Press Event Example</h2> <input type="text" placeholder="Press a key" onKeyPress={this.handleKeyPress} /> </div> ); } } export default KeyPressExample;
App.js
import React from 'react'; import './App.css'; import EventHandlingExample from './EventHandlingExample'; import EventMethodsExample from './EventMethodsExample'; import KeyPressExample from './KeyPressExample'; // Import the new component function App() { return ( <div> <h1>React Event Examples</h1> <EventHandlingExample /> <EventMethodsExample /> <KeyPressExample /> {/* Include the new component */} </div> ); } export default App;
輸出

結論
簡單來說,React 事件物件幫助我們以可靠且有效的方式處理使用者互動,確保我們的應用程式在多個 Web 瀏覽器和設定中都能正常執行。