網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
React點(diǎn)擊事件寫(xiě)法
1.bind綁定(推薦)
第一個(gè)參數(shù)指向this,第二個(gè)參數(shù)開(kāi)始才是事件函數(shù)接收到的參數(shù),事件對(duì)象event默認(rèn)是最后一個(gè)參數(shù)。
...
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")}>點(diǎn)擊</button>
? ? ? ? </React.Fragment>
? ? )
}
...
這里的話(huà)綁定this可以統(tǒng)一寫(xiě),這樣代碼看起來(lái)整潔點(diǎn)。
...
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')}>點(diǎn)擊</button>
? ? ? ? </React.Fragment>
? ? )
}
...
2.箭頭函數(shù)
箭頭函數(shù)若要傳事件對(duì)象event的話(huà),需要在箭頭函數(shù)中把event作為參數(shù)傳遞給觸發(fā)的事件。
...
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)}>點(diǎn)擊</button>
? ? ? ? </React.Fragment>
? ? )
}
...
React點(diǎn)擊事件不好用,可能是被其他組件遮擋
入門(mén)React不久,練習(xí)例子的時(shí)候發(fā)現(xiàn)點(diǎn)擊退出事件不好用。
而邏輯啥的沒(méi)有問(wèn)題,在點(diǎn)擊事件里寫(xiě)console打印也沒(méi)反應(yīng)(就是根本不識(shí)別)。
搜索一下,發(fā)現(xiàn)可能是按鈕所在的組件被底部導(dǎo)航遮擋住了。
給導(dǎo)航的less樣式添加“z-index:-1”便可以使用了。
原文鏈接:https://blog.csdn.net/weixin_39782183/article/details/104751831
相關(guān)推薦
- 2022-03-20 C++?Qt繪制時(shí)鐘界面_C 語(yǔ)言
- 2022-10-07 k8s中如何實(shí)現(xiàn)pod自動(dòng)擴(kuò)縮容詳解_云其它
- 2023-05-20 Golang中map的三種聲明定義方式實(shí)現(xiàn)_Golang
- 2022-11-17 Go語(yǔ)言學(xué)習(xí)教程之指針的示例詳解_Golang
- 2023-07-04 解決Uncaught (in promise) TypeError: Cannot read pro
- 2022-10-22 ListView下拉列表控件使用方法詳解_Android
- 2022-06-01 利用Python實(shí)現(xiàn)外觀(guān)數(shù)列求解_python
- 2022-06-29 python人工智能tensorflow函數(shù)tf.layers.dense使用方法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支