- 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 - 在支出管理器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 - Reconciliation
- 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 - 討論
在支出管理器APP中介紹事件
在前面的章節中,我們瞭解到事件只是使用者與任何應用程式互動時執行的一些操作。它們可以是最小的操作,例如將滑鼠指標懸停在觸發下拉選單的元素上、調整應用程式視窗大小或拖放元素以上傳它們等。
對於這些事件中的每一個,JavaScript都提供了響應。因此,每次使用者執行事件時,通常都需要應用程式做出某種型別的反應;這些反應被定義為一些函式或程式碼塊,稱為事件處理程式。
為了更好地理解事件處理,讓我們嘗試在一個示例應用程式(支出管理器APP)中介紹事件。在本節中,我們將嘗試在支出管理器應用程式中處理滑鼠事件。滑鼠事件只是使用者使用滑鼠執行的一些操作。這些包括懸停、單擊、拖動或任何可以使用滑鼠在應用程式上執行的操作。
在支出管理器APP中處理事件
讓我們在支出應用程式中進行一些事件管理。當用戶將游標移到表格上的支出條目時,我們可以嘗試突出顯示錶格中的支出條目。
步驟1 - 在您喜歡的編輯器中開啟expense-manager應用程式。
開啟ExpenseEntryItemList.js檔案,並新增一個方法handleMouseEnter來處理當用戶將滑鼠指標移動到支出專案(td - 表格單元格)時觸發的事件(onMouseEnter)。
handleMouseEnter(e) {
e.target.parentNode.classList.add("highlight");
}
這裡,
事件處理程式嘗試使用parentNode方法查詢事件目標(td)節點的父節點(tr)。parentNode方法是查詢當前節點的直接父節點的標準DOM方法。
找到父節點後,事件處理程式訪問附加到父節點的css類的列表,並使用add方法新增'highlight'類。classList是獲取附加到節點的類列表的標準DOM屬性,它可以用來向DOM節點新增/刪除類。
步驟2 - 接下來,新增一個方法handleMouseLeave()來處理使用者從支出專案移出時觸發的事件。
handleMouseLeave(e) {
e.target.parentNode.classList.remove("highlight");
}
在這裡,事件處理程式從DOM中刪除highlight類。
新增另一個方法handleMouseOver()來檢查滑鼠當前所在位置。在DOM中查詢滑鼠指標的位置是可選的。
handleMouseOver(e) {
console.log("The mouse is at (" + e.clientX + ", " + e.clientY + ")");
}
步驟3 - 在元件的建構函式中繫結所有事件處理程式。
this.handleMouseEnter = this.handleMouseEnter.bind(); this.handleMouseLeave = this.handleMouseLeave.bind(); this.handleMouseOver = this.handleMouseOver.bind();
步驟4 - 將事件處理程式附加到render方法中的相應標籤。
render() {
const lists = this.props.items.map((item) =>
<tr key={item.id} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
<td>{item.name}</td>
<td>{item.amount}</td>
<td>{new Date(item.spendDate).toDateString()}</td>
<td>{item.category}</td>
</tr>
);
return (
<table onMouseOver={this.handleMouseOver}>
<thead>
<tr>
<th>Item</th>
<th>Amount</th>
<th>Date</th>
<th>Category</th>
</tr>
</thead>
<tbody>
{lists}
</tbody>
</table>
);
}
ExpenseEntryItemList的最終完整程式碼如下:
import React from 'react';
import './ExpenseEntryItemList.css';
class ExpenseEntryItemList extends React.Component {
constructor(props) {
super(props);
this.handleMouseEnter = this.handleMouseEnter.bind();
this.handleMouseLeave = this.handleMouseLeave.bind();
this.handleMouseOver = this.handleMouseOver.bind();
}
handleMouseEnter(e) {
e.target.parentNode.classList.add("highlight");
}
handleMouseLeave(e) {
e.target.parentNode.classList.remove("highlight");
}
handleMouseOver(e) {
console.log("The mouse is at (" + e.clientX + ", " + e.clientY + ")");
}
render() {
const lists = this.props.items.map((item) =>
<tr key={item.id} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
<td>{item.name}</td>
<td>{item.amount}</td>
<td>{new Date(item.spendDate).toDateString()}</td>
<td>{item.category}</td>
</tr>
);
return (
<table onMouseOver={this.handleMouseOver}>
<thead>
<tr>
<th>Item</th>
<th>Amount</th>
<th>Date</th>
<th>Category</th>
</tr>
</thead>
<tbody>
{lists}
</tbody>
</table>
);
}
}
export default ExpenseEntryItemList;
ExpenseEntryItemList.css
接下來,開啟css檔案ExpenseEntryItemList.css並新增一個css類highlight。
tr.highlight td {
background-color: #a6a8bd;
}
index.js
開啟index.js並使用ExpenseEntryItemList元件。
import React from 'react';
import ReactDOM from 'react-dom';
import ExpenseEntryItemList from './components/ExpenseEntryItemList'
const items = [
{ id: 1, name: "Pizza", amount: 80, spendDate: "2020-10-10", category: "Food" },
{ id: 2, name: "Grape Juice", amount: 30, spendDate: "2020-10-12", category: "Food" },
{ id: 3, name: "Cinema", amount: 210, spendDate: "2020-10-16", category: "Entertainment" },
{ id: 4, name: "Java Programming book", amount: 242, spendDate: "2020-10-15", category: "Academic" },
{ id: 5, name: "Mango Juice", amount: 35, spendDate: "2020-10-16", category: "Food" },
{ id: 6, name: "Dress", amount: 2000, spendDate: "2020-10-25", category: "Cloth" },
{ id: 7, name: "Tour", amount: 2555, spendDate: "2020-10-29", category: "Entertainment" },
{ id: 8, name: "Meals", amount: 300, spendDate: "2020-10-30", category: "Food" },
{ id: 9, name: "Mobile", amount: 3500, spendDate: "2020-11-02", category: "Gadgets" },
{ id: 10, name: "Exam Fees", amount: 1245, spendDate: "2020-11-04", category: "Academic" }
]
ReactDOM.render(
<React.StrictMode>
<ExpenseEntryItemList items={items} />
</React.StrictMode>,
document.getElementById('root')
);
使用npm命令啟動應用程式。
npm start
開啟瀏覽器並在位址列中輸入https://:3000,然後按回車鍵。
應用程式將響應滑鼠事件並突出顯示當前選擇的行。
| 專案 | 金額 | 日期 | 類別 |
| 披薩 | 80 | 2020年10月10日 星期六 | 食品 |
| 葡萄汁 | 30 | 2020年10月12日 星期一 | 食品 |
| 電影 | 210 | 2020年10月16日 星期五 | 娛樂 |
| Java程式設計書籍 | 242 | 2020年10月15日 星期四 | 學術 |
| 芒果汁 | 35 | 2020年10月16日 星期五 | 食品 |
| 衣服 | 2000 | 2020年10月25日 星期日 | 服裝 |
| 旅遊 | 2555 | 2020年10月29日 星期四 | 娛樂 |
| 餐飲 | 300 | 2020年10月30日 星期五 | 食品 |
| 手機 | 3500 | 2020年11月2日 星期一 | 電子產品 |
| 考試費 | 1245 | 2020年11月4日 星期三 | 學術 |