網站首頁 編程語言 正文
React事件的類型定義
基本的事件類型
React 搞了一套自己的事件類型,所以你沒辦法使用 TypeScript 內置提供的 MouseEvent 等等。在需要定義事件類型的時候,需要從 React 中導入:
import React, { Component, MouseEvent } from 'react'; export class Button extends Component { ? handleClick(event: MouseEvent) { ? ? event.preventDefault(); ? ? alert(event.currentTarget.tagName); // alerts BUTTON ? } ?? ? render() { ? ? return ( ? ? <button onClick={this.handleClick}> ? ? ? {this.props.children} ? ? </button> ? ? ); ? } }
React 提供的事件類型有:
AnimationEvent?
ChangeEvent?
ClipboardEvent?
CompositionEvent?
DragEvent?
FocusEvent?
FormEvent?
KeyboardEvent?
MouseEvent?
PointerEvent?
TouchEvent?
TransitionEvent?
WheelEvent
還有一個 SyntheticEvent,用于其他所有的事件。
限制性的事件類型
如果需要限制事件類型,可以利用事件類型的泛型:
import React, { Component, MouseEvent } from 'react'; export class Button extends Component { ? /* ? ?* 這里我們將 handleClick 限制只能是在 HTMLButton 元素上 ? ?*/ ? handleClick(event: MouseEvent<HTMLButtonElement>) { ? ? event.preventDefault(); ? ? alert(event.currentTarget.tagName); // alerts BUTTON ? } ? /*? ? ?* 支持聯合類型 ? ?*/ ? handleAnotherClick(event: MouseEvent<HTMLButtonElement | HTMLAnchorElement>) { ? ? event.preventDefault(); ? ? alert('Yeah!'); ? } ? render() { ? ? return <button onClick={this.handleClick}> ? ? ? {this.props.children} ? ? </button> ? } }
這里的限定的類型是 TypeScript 提供的 DOM 元素類型。
React四種定義事件方式
事件本身處的位置是一個屬性 如果屬性的值是一個函數
使用{}包裹,一定要保證該函數內的this指向
import React, { Component } from 'react'; export default class App extends Component { constructor(props) { super(props); this.add1000Fn = this.add1000.bind(this) } state = { count:1 } add10 = () => { this.setState({count:this.state.count + 10}) } add100 = function() { // 一般寫為 add100 () {} 二者是等價的 console.log(this); this.setState({count:this.state.count + 100}) } add1000() { this.setState({count:this.state.count + 1000}) } render() { return ( <> <div>count:{ this.state.count}</div> {/* 1.事件定義方式1:直接在render里寫行內的箭頭函數 */} <button onClick={ ()=>{ //如果該函數使用聲明式 function(){} 內部的this指向為undefined this.setState({count:++this.state.count}) }}>加一</button> {/* 2.事件定義方式2:在組件內使用箭頭函數定義的一個方法 */} <button onClick={this.add10}>加十</button> {/* 3.事件定義方式3:直接在組件內定義一個非箭頭函數的方法,然后在render里直接使用`onClick={this.handleClick.bind(this)}` */} <button onClick={ this.add100.bind(this)}>加一百</button> {/* 4.事件定義方式4: 直接在組件內定義一個非箭頭函數的方法,然后在constructor里bind(this)*/} <button onClick = { this.add1000Fn }>加1000</button> </> ); } }
總結
原文鏈接:https://blog.csdn.net/YopenLang/article/details/125910670
相關推薦
- 2022-05-26 Python學習之文件的讀取詳解_python
- 2022-12-05 關于EF的Code?First的使用以及踩坑記錄_實用技巧
- 2022-07-13 C# System.Web.HttpContext.Current.Server.MapPath 報
- 2023-04-03 iOS數據持久化KeyChain數據操作詳解_IOS
- 2022-08-29 Python?GUI?圖形用戶界面_python
- 2023-01-09 pip升級pip3的快速方法指南_python
- 2023-06-18 C#中獲取文件大小問題_C#教程
- 2022-08-10 C#實現同步模式下的端口映射程序_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支