網站首頁 編程語言 正文
react的onClick自動觸發等問題
react分頁組件遇到的問題:
? private getFirst() {
? ? const { pageNo } = this.state;
? ? if (pageNo > 3) {
? ? ? return (<span onClick={this.changePage(1)}>首頁</span>);
? ? } else {
? ? ? return;
? ? }
? }
? private changePage (index: number) {
? ? this.setState({ pageNo: index });
? }
這段代碼的onClick直接報錯:不能將類型“void”分配給類型“((event: MouseEvent<HTMLSpanElement>) => void) | undefined”,這個報錯看得我莫名其妙……
同事告訴我要給changePage函數加個return ()=>{}形成閉包。其實后來我才發現,雖然changePage寫得很像function,但其只是一個變量而已,所以并不滿足onClick的內容是方法的情況。
? private getFirst() {
? ? const { pageNo } = this.state;
? ? if (pageNo > 3) {
? ? ? return (<span onClick={this.changePage(1)}>首頁</span>);
? ? } else {
? ? ? return;
? ? }
? }
?
? private changePage (index: number) {
? ? return () => {this.setState({ pageNo: index });}
? }
這樣寫雖然不報錯,但有個嚴重的問題,每次生成UI時它都會自動觸發onClick,這個機制真是???……所以最后改成了:
? private getFirst() {
? ? const { pageNo } = this.state;
? ? if (pageNo > 3) {
? ? ? return (<span onClick={()=>this.changePage(1)}>首頁</span>);
? ? } else {
? ? ? return;
? ? }
? }
? private changePage (index: number) {
? ? this.setState({ pageNo: index });
? }
再補充一個使用的例子:
import React from 'react';
?
interface Iprops {
? name: string;
? click: any;
}
?
const ButtonItem = (props: Iprops) => {
? return <div onClick={props.click}>{props.name}</div>
}
?
const ContentMenu: React.FC<{}> = () => {
? const product = (type: number) => {
? ? return () => {
? ? ? alert(type);
? ? }
? }
?
? return (
? ? <div>
? ? ? <ButtonItem name="生成1" click={product(1)} />
? ? </div>
? );
};
?
export default ContentMenu;
react的onClick事件調用的各種寫法與觸發情況
handleClick 是普通函數
handleClick(params?: any) {
console.log('1000101010101010100');
console.log(params);
console.log(this)
}
onClick={this.handleClick}
// 正確,但是無法傳值
onClick={this.handleClick()}
// 在handleClick(): any,即聲明返回值any 時,不需要 this.handleClick = this.handleClick.bind(this),似乎能正確使用,能獲取到 this;
// 在handleClick() 不設置 : any 時,不管是否增加 this.handleClick = this.handleClick.bind(this),都是報錯,界面無法顯示。
onClick={this.handleClick.bind(this, 12)}
// 正確,不需要 this.handleClick = this.handleClick.bind(this),能獲取this,能獲取參數。
onClick={() => this.handleClick}
// 無效且無報錯,即無法觸發
onClick={() => { this.handleClick }}
// 無效且無報錯,即無法觸發
onClick={() => this.handleClick()}
// 正確,不需要 this.handleClick = this.handleClick.bind(this),能獲取this,能獲取參數。
onClick={() => { this.handleClick() }}
// 正確,能獲取this,能獲取參數。
handleClick 是箭頭函數
PS:箭頭函數皆不需要 this.handleClick = this.handleClick.bind(this):
handleClick = (params?: any) => {
console.log('1000101010101010100');
console.log(params);
console.log(this)
}
onClick={this.handleClick}
// 正確,但是無法傳值
onClick={this.handleClick()}
// 在handleClick(): any,即聲明返回值any 時,頁面渲染時,自動觸發onClick,且onClick點擊失效;
// 在handleClick() 不設置 : any 時,報錯,界面無法顯示。
onClick={() => this.handleClick}
// 無效且無報錯,即無法觸發
onClick={() => { this.handleClick }}
// 無效且無報錯,即無法觸發
onClick={() => this.handleClick()}
// 正確,能獲取this,能獲取參數。
onClick={() => { this.handleClick() }}
// 正確,能獲取this,能獲取參數。
總結
原文鏈接:https://blog.csdn.net/guozhicaice/article/details/86701099
相關推薦
- 2022-02-13 使用paddlepaddle進行手寫數字識別
- 2022-04-10 Android中Protobuf的基本使用介紹_Android
- 2022-04-05 android使用shape自定義Switch組件
- 2022-01-03 antd獲取表單的所有數據
- 2022-05-06 python使用xlrd模塊讀取excel的方法實例_python
- 2022-04-09 C#實現計算器精簡版_C#教程
- 2022-03-19 nginx?rtmp模塊編譯?arm版本的問題_nginx
- 2022-04-21 R語言數據可視化繪圖Slope?chart坡度圖畫法_R語言
- 最近更新
-
- 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同步修改后的遠程分支