網站首頁 編程語言 正文
React點擊事件寫法
1.bind綁定(推薦)
第一個參數指向this,第二個參數開始才是事件函數接收到的參數,事件對象event默認是最后一個參數。
...
clicked(param,event){
? ? console.log(param) //hello world
? ? console.log(event.target.value) //按鈕
}
render(){
? ? return (
? ? ? ? <React.Fragment>
? ? ? ? ? ? <button value="按鈕" onClick={this.clicked.bind(this,"hello world")}>點擊</button>
? ? ? ? </React.Fragment>
? ? )
}
...
這里的話綁定this可以統一寫,這樣代碼看起來整潔點。
...
constructor(props){
? ? super(props);
? ? this.state = {};
? ? this.checkMenu = this.checkMenu.bind(this);
}
clicked = (param)=>{
? ? return (event)=>{
? ? ? ? console.log(event.target.value); // 按鈕
? ? ? ? console.log(param); // hello
? ? }
}
render(){
? ? return (
? ? ? ? <React.Fragment>
? ? ? ? ? ? <button value="按鈕" onClick={this.clicked('hello')}>點擊</button>
? ? ? ? </React.Fragment>
? ? )
}
...
2.箭頭函數
箭頭函數若要傳事件對象event的話,需要在箭頭函數中把event作為參數傳遞給觸發的事件。
...
clicked(param,event){
? ? console.log(param) //hello world
? ? console.log(event.target.value) //按鈕
}
render(){
? ? return (
? ? ? ? <React.Fragment>
? ? ? ? ? ? <button value="按鈕" onClick={(event)=>this.clicked("hello world",event)}>點擊</button>
? ? ? ? </React.Fragment>
? ? )
}
...
React點擊事件不好用,可能是被其他組件遮擋
入門React不久,練習例子的時候發現點擊退出事件不好用。
而邏輯啥的沒有問題,在點擊事件里寫console打印也沒反應(就是根本不識別)。
搜索一下,發現可能是按鈕所在的組件被底部導航遮擋住了。
給導航的less樣式添加“z-index:-1”便可以使用了。
原文鏈接:https://blog.csdn.net/weixin_39782183/article/details/104751831
相關推薦
- 2022-01-15 linux 安裝插件報錯:Loaded plugins: fastestmirror
- 2022-07-04 如何用python實現結構體數組_python
- 2022-10-16 python列表倒序的幾種方法(切片、reverse()、reversed())_python
- 2022-08-05 Python?list列表查找元素詳情_python
- 2022-10-17 docker網絡及部署集群和打包鏡像問題_docker
- 2022-09-25 Identity Server4/生產模式/證書/certificate/AddSigningCre
- 2021-12-10 C語言光標信息CONSOLE_CURSOR_INFO類型詳解_C 語言
- 2023-02-06 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同步修改后的遠程分支