news 2026/4/18 9:45:27

React 元素的事件处理和 DOM 元素类似

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
React 元素的事件处理和 DOM 元素类似

React 事件处理

React 元素的事件处理和 DOM 元素类似。但是有一点语法上的不同:

  • React 事件绑定属性的命名采用驼峰式写法,而不是小写。
  • 如果采用 JSX 的语法你需要传入一个函数作为事件处理函数,而不是一个字符串(DOM 元素的写法)

HTML 通常写法是:

<button onclick="activateLasers()"> 激活按钮 </button>

React 中写法为:

<button onClick={activateLasers}> 激活按钮 </button>

在 React 中另一个不同是你不能使用返回false的方式阻止默认行为, 你必须明确使用 preventDefault。

例如,通常我们在 HTML 中阻止链接默认打开一个新页面,可以这样写:

<a href="#" onclick="console.log('点击链接'); return false"> 点我 </a>

在 React 的写法为:

function ActionLink() { function handleClick(e) { e.preventDefault(); console.log('链接被点击'); } return ( <a href="#" onClick={handleClick}> 点我 </a> ); }

实例中 e 是一个合成事件。

使用 React 的时候通常你不需要使用 addEventListener 为一个已创建的 DOM 元素添加监听器。你仅仅需要在这个元素初始渲染的时候提供一个监听器。

当你使用 ES6 class 语法来定义一个组件的时候,事件处理器会成为类的一个方法。例如,下面的 Toggle 组件渲染一个让用户切换开关状态的按钮:

实例

class Toggle extends React.Component { constructor(props) { super(props); this.state = {isToggleOn: true}; // 这边绑定是必要的,这样 `this` 才能在回调函数中使用 this.handleClick = this.handleClick.bind(this); } handleClick() { this.setState(prevState => ({ isToggleOn: !prevState.isToggleOn })); } render() { return ( <button onClick={this.handleClick}> {this.state.isToggleOn ? 'ON' : 'OFF'} </button> ); } } const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <Toggle />, document.getElementById('example') );

你必须谨慎对待 JSX 回调函数中的 this,类的方法默认是不会绑定 this 的。如果你忘记绑定 this.handleClick 并把它传入 onClick, 当你调用这个函数的时候 this 的值会是 undefined。

这并不是 React 的特殊行为;它是函数如何在 JavaScript 中运行的一部分。通常情况下,如果你没有在方法后面添加 () ,例如 onClick={this.handleClick},你应该为这个方法绑定 this。

如果使用 bind 让你很烦,这里有两种方式可以解决。如果你正在使用实验性的属性初始化器语法,你可以使用属性初始化器来正确的绑定回调函数:

class LoggingButton extends React.Component { // 这个语法确保了 `this` 绑定在 handleClick 中 // 这里只是一个测试 handleClick = () => { console.log('this is:', this); } render() { return ( <button onClick={this.handleClick}> Click me </button> ); } }

如果你没有使用属性初始化器语法,你可以在回调函数中使用 箭头函数:

class LoggingButton extends React.Component { handleClick() { console.log('this is:', this); } render() { // 这个语法确保了 `this` 绑定在 handleClick 中 return ( <button onClick={(e) => this.handleClick(e)}> Click me </button> ); } }

使用这个语法有个问题就是每次 LoggingButton 渲染的时候都会创建一个不同的回调函数。在大多数情况下,这没有问题。然而如果这个回调函数作为一个属性值传入低阶组件,这些组件可能会进行额外的重新渲染。我们通常建议在构造函数中绑定或使用属性初始化器语法来避免这类性能问题。


向事件处理程序传递参数

通常我们会为事件处理程序传递额外的参数。例如,若是 id 是你要删除那一行的 id,以下两种方式都可以向事件处理程序传递参数:

<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button> <button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>

上述两种方式是等价的。

https://avg.163.com/topic/detail/9240046
https://avg.163.com/topic/detail/9240085
https://avg.163.com/topic/detail/9240116
https://avg.163.com/topic/detail/9240160
https://avg.163.com/topic/detail/9240195
https://avg.163.com/topic/detail/9240043
https://avg.163.com/topic/detail/9240086
https://avg.163.com/topic/detail/9240045
https://avg.163.com/topic/detail/9240118
https://avg.163.com/topic/detail/9240084
https://avg.163.com/topic/detail/9240146
https://avg.163.com/topic/detail/9240115
https://avg.163.com/topic/detail/9240174
https://avg.163.com/topic/detail/9240144
https://avg.163.com/topic/detail/9240044
https://avg.163.com/topic/detail/9240088
https://avg.163.com/topic/detail/9240124
https://avg.163.com/topic/detail/9240158
https://avg.163.com/topic/detail/9240193
https://avg.163.com/topic/detail/9234504
https://avg.163.com/topic/detail/9234511
https://avg.163.com/topic/detail/9234496
https://avg.163.com/topic/detail/9234503
https://avg.163.com/topic/detail/9234509
https://avg.163.com/topic/detail/9234493
https://avg.163.com/topic/detail/9234494
https://avg.163.com/topic/detail/9234499
https://avg.163.com/topic/detail/9234507
https://avg.163.com/topic/detail/9234491
https://avg.163.com/topic/detail/9234501
https://avg.163.com/topic/detail/9234508
https://avg.163.com/topic/detail/9234495
https://avg.163.com/topic/detail/9234506
https://avg.163.com/topic/detail/9234492
https://avg.163.com/topic/detail/9234486
https://avg.163.com/topic/detail/9234487
https://avg.163.com/topic/detail/9234488

上面两个例子中,参数 e 作为 React 事件对象将会被作为第二个参数进行传递。通过箭头函数的方式,事件对象必须显式的进行传递,但是通过 bind 的方式,事件对象以及更多的参数将会被隐式的进行传递。

值得注意的是,通过 bind 方式向监听函数传参,在类组件中定义的监听函数,事件对象 e 要排在所传递参数的后面,例如:

https://avg.163.com/topic/detail/9240700
https://avg.163.com/topic/detail/9241029
https://avg.163.com/topic/detail/9240565
https://avg.163.com/topic/detail/9240926
https://avg.163.com/topic/detail/9241048
https://avg.163.com/topic/detail/9241069
https://avg.163.com/topic/detail/9240653
https://avg.163.com/topic/detail/9240773
https://avg.163.com/topic/detail/9241035
https://avg.163.com/topic/detail/9240714
https://avg.163.com/topic/detail/9240981
https://avg.163.com/topic/detail/9241006
https://avg.163.com/topic/detail/9240724
https://avg.163.com/topic/detail/9240536
https://avg.163.com/topic/detail/9240583
https://avg.163.com/topic/detail/9240659
https://avg.163.com/topic/detail/9240695
https://avg.163.com/topic/detail/9240800
https://avg.163.com/topic/detail/9241068
https://avg.163.com/topic/detail/9240754
https://avg.163.com/topic/detail/9240584
https://avg.163.com/topic/detail/9240956
https://avg.163.com/topic/detail/9240543
https://avg.163.com/topic/detail/9240627
https://avg.163.com/topic/detail/9240671
https://avg.163.com/topic/detail/9240699
https://avg.163.com/topic/detail/9240730
https://avg.163.com/topic/detail/9240826
https://avg.163.com/topic/detail/9240794
https://avg.163.com/topic/detail/9240538
https://avg.163.com/topic/detail/9240762
https://avg.163.com/topic/detail/9240793
https://avg.163.com/topic/detail/9240827
https://avg.163.com/topic/detail/9240847
https://avg.163.com/topic/detail/9240747
https://avg.163.com/topic/detail/9240923
https://avg.163.com/topic/detail/9240955
https://avg.163.com/topic/detail/9241032
https://avg.163.com/topic/detail/9240658
https://avg.163.com/topic/detail/9240980
https://avg.163.com/topic/detail/9241005
https://avg.163.com/topic/detail/9241030
https://avg.163.com/topic/detail/9240715
https://avg.163.com/topic/detail/9240738
https://avg.163.com/topic/detail/9240775
https://avg.163.com/topic/detail/9240983
https://avg.163.com/topic/detail/9241063
https://avg.163.com/topic/detail/9240702
https://avg.163.com/topic/detail/9240750
https://avg.163.com/topic/detail/9240784
https://avg.163.com/topic/detail/9241056
https://avg.163.com/topic/detail/9240756
https://avg.163.com/topic/detail/9240856
https://avg.163.com/topic/detail/9240825
https://avg.163.com/topic/detail/9240600
https://avg.163.com/topic/detail/9240638
https://avg.163.com/topic/detail/9240687
https://avg.163.com/topic/detail/9240818
https://avg.163.com/topic/detail/9240848
https://avg.163.com/topic/detail/9240804
https://avg.163.com/topic/detail/9241007
https://avg.163.com/topic/detail/9241034
https://avg.163.com/topic/detail/9240836
https://avg.163.com/topic/detail/9240862
https://avg.163.com/topic/detail/9240880
https://avg.163.com/topic/detail/9240881
https://avg.163.com/topic/detail/9240835
https://avg.163.com/topic/detail/9240854
https://avg.163.com/topic/detail/9241064
https://avg.163.com/topic/detail/9240573
https://avg.163.com/topic/detail/9240765
https://avg.163.com/topic/detail/9240789
https://avg.163.com/topic/detail/9240884
https://avg.163.com/topic/detail/9240865
https://avg.163.com/topic/detail/9240899
https://avg.163.com/topic/detail/9240928
https://avg.163.com/topic/detail/9240664
https://avg.163.com/topic/detail/9240806
https://avg.163.com/topic/detail/9240828
https://avg.163.com/topic/detail/9240911
https://avg.163.com/topic/detail/9240940
https://avg.163.com/topic/detail/9240964
https://avg.163.com/topic/detail/9240988
https://avg.163.com/topic/detail/9240957
https://avg.163.com/topic/detail/9240710
https://avg.163.com/topic/detail/9240843
https://avg.163.com/topic/detail/9240857
https://avg.163.com/topic/detail/9240889
https://avg.163.com/topic/detail/9240874
https://avg.163.com/topic/detail/9240894
https://avg.163.com/topic/detail/9240749
https://avg.163.com/topic/detail/9240877
https://avg.163.com/topic/detail/9240579
https://avg.163.com/topic/detail/9240932
https://avg.163.com/topic/detail/9240909
https://avg.163.com/topic/detail/9240919
https://avg.163.com/topic/detail/9240783
https://avg.163.com/topic/detail/9240907
https://avg.163.com/topic/detail/9240625
https://avg.163.com/topic/detail/9240985
https://avg.163.com/topic/detail/9240918
https://avg.163.com/topic/detail/9240886
https://avg.163.com/topic/detail/9240933
https://avg.163.com/topic/detail/9240670
https://avg.163.com/topic/detail/9240555
https://avg.163.com/topic/detail/9240951
https://avg.163.com/topic/detail/9240817
https://avg.163.com/topic/detail/9241013
https://avg.163.com/topic/detail/9240938
https://avg.163.com/topic/detail/9241011
https://avg.163.com/topic/detail/9240950
https://avg.163.com/topic/detail/9240913
https://avg.163.com/topic/detail/9240572
https://avg.163.com/topic/detail/9240545
https://avg.163.com/topic/detail/9240711
https://avg.163.com/topic/detail/9240599
https://avg.163.com/topic/detail/9240975
https://avg.163.com/topic/detail/9240943
https://avg.163.com/topic/detail/9240612
https://avg.163.com/topic/detail/9240581
https://avg.163.com/topic/detail/9240753
https://avg.163.com/topic/detail/9240578
https://avg.163.com/topic/detail/9240973
https://avg.163.com/topic/detail/9240967
https://avg.163.com/topic/detail/9240652
https://avg.163.com/topic/detail/9241043
https://avg.163.com/topic/detail/9240643
https://avg.163.com/topic/detail/9240986
https://avg.163.com/topic/detail/9241044
https://avg.163.com/topic/detail/9240963
https://avg.163.com/topic/detail/9240791
https://avg.163.com/topic/detail/9240624
https://avg.163.com/topic/detail/9240998
https://avg.163.com/topic/detail/9241066
https://avg.163.com/topic/detail/9240556
https://avg.163.com/topic/detail/9241065
https://avg.163.com/topic/detail/9240570
https://avg.163.com/topic/detail/9240667
https://avg.163.com/topic/detail/9241021
https://avg.163.com/topic/detail/9241000
https://avg.163.com/topic/detail/9240842
https://avg.163.com/topic/detail/9240602
https://avg.163.com/topic/detail/9240713
https://avg.163.com/topic/detail/9241051
https://avg.163.com/topic/detail/9241025
https://avg.163.com/topic/detail/9240875
https://avg.163.com/topic/detail/9240633
https://avg.163.com/topic/detail/9240757
https://avg.163.com/topic/detail/9240682
https://avg.163.com/topic/detail/9241003
https://avg.163.com/topic/detail/9240688
https://avg.163.com/topic/detail/9240587
https://avg.163.com/topic/detail/9241054
https://avg.163.com/topic/detail/9240931
https://avg.163.com/topic/detail/9240697
https://avg.163.com/topic/detail/9240787
https://avg.163.com/topic/detail/9240725
https://avg.163.com/topic/detail/9240958
https://avg.163.com/topic/detail/9240732
https://avg.163.com/topic/detail/9240567
https://avg.163.com/topic/detail/9241028
https://avg.163.com/topic/detail/9240736
https://avg.163.com/topic/detail/9240636
https://avg.163.com/topic/detail/9240821
https://avg.163.com/topic/detail/9241055
https://avg.163.com/topic/detail/9240779
https://avg.163.com/topic/detail/9240689
https://avg.163.com/topic/detail/9240864
https://avg.163.com/topic/detail/9240776
https://avg.163.com/topic/detail/9240591
https://avg.163.com/topic/detail/9240987
https://avg.163.com/topic/detail/9240813
https://avg.163.com/topic/detail/9240728
https://avg.163.com/topic/detail/9240893
https://avg.163.com/topic/detail/9240811
https://avg.163.com/topic/detail/9240631
https://avg.163.com/topic/detail/9240916
https://avg.163.com/topic/detail/9240850
https://avg.163.com/topic/detail/9240694
https://avg.163.com/topic/detail/9240948
https://avg.163.com/topic/detail/9240845
https://avg.163.com/topic/detail/9241020
https://avg.163.com/topic/detail/9241060
https://avg.163.com/topic/detail/9240976
https://avg.163.com/topic/detail/9240878
https://avg.163.com/topic/detail/9241001
https://avg.163.com/topic/detail/9240905
https://avg.163.com/topic/detail/9241023
https://avg.163.com/topic/detail/9240930
https://avg.163.com/topic/detail/9241046
https://avg.163.com/topic/detail/9240960
https://avg.163.com/topic/detail/9240995
https://avg.163.com/topic/detail/9241017
https://avg.163.com/topic/detail/9241057
https://avg.163.com/topic/detail/9240557
https://avg.163.com/topic/detail/9240489
https://avg.163.com/topic/detail/9240477
https://avg.163.com/topic/detail/9240498
https://avg.163.com/topic/detail/9240511
https://avg.163.com/topic/detail/9240525
https://avg.163.com/topic/detail/9240475
https://avg.163.com/topic/detail/9240485
https://avg.163.com/topic/detail/9240496
https://avg.163.com/topic/detail/9240518
https://avg.163.com/topic/detail/9240483
https://avg.163.com/topic/detail/9240503
https://avg.163.com/topic/detail/9240515
https://avg.163.com/topic/detail/9240523
https://avg.163.com/topic/detail/9240494
https://avg.163.com/topic/detail/9240504
https://avg.163.com/topic/detail/9240527
https://avg.163.com/topic/detail/9240476
https://avg.163.com/topic/detail/9240486
https://avg.163.com/topic/detail/9240514
https://avg.163.com/topic/detail/9240521
https://avg.163.com/topic/detail/9240480
https://avg.163.com/topic/detail/9240490
https://avg.163.com/topic/detail/9240499
https://avg.163.com/topic/detail/9240509
https://avg.163.com/topic/detail/9240488
https://avg.163.com/topic/detail/9240501
https://avg.163.com/topic/detail/9240513
https://avg.163.com/topic/detail/9240524
https://avg.163.com/topic/detail/9240478
https://avg.163.com/topic/detail/9240484
https://avg.163.com/topic/detail/9240495
https://avg.163.com/topic/detail/9240506
https://avg.163.com/topic/detail/9240491
https://avg.163.com/topic/detail/9240502
https://avg.163.com/topic/detail/9240473
https://avg.163.com/topic/detail/9240512
https://avg.163.com/topic/detail/9240487
https://avg.163.com/topic/detail/9240520
https://avg.163.com/topic/detail/9240497
https://avg.163.com/topic/detail/9240507
https://avg.163.com/topic/detail/9240519

class Popper extends React.Component{ constructor(){ super(); this.state = {name:'Hello world!'}; } preventPop(name, e){ //事件对象e要放在最后 e.preventDefault(); alert(name); } render(){ return ( <div> <p>hello</p> {/* 通过 bind() 方法传递参数。 */} <a href="https://reactjs.org" onClick={this.preventPop.bind(this,this.state.name)}>Click</a> </div> ); } }

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 5:31:56

你看到的人不舒服,就是不好,远离就行,或者不说话不表态:你是世界和核心,没有你,世界将不在存在

“驭人术”清单,把职场的脏底裤都扒下来了 目录 “驭人术”清单,把职场的脏底裤都扒下来了 你看到的人不舒服,就是不好,远离就行,或者不说话不表态 你是世界和核心,没有你,世界将不在存在 这些“驭人术”,本质是职场PUA的变种 真正的带人,从来不是“驭人” 打工人该怎…

作者头像 李华
网站建设 2026/4/18 5:35:50

stm32单片机智能浇花浇水土壤湿度光照检测远程控制系统设计成品

目录系统概述硬件组成软件设计功能特点应用场景注意事项源码文档获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01;系统概述 STM32单片机智能浇花系统是一款基于土壤湿度、光照强度检测的自动化灌溉装置&#xff0c;支持远程控制。该系统通过传感…

作者头像 李华
网站建设 2026/4/18 0:06:36

学长亲荐!专科生毕业论文必备!8款一键生成论文工具TOP8测评

学长亲荐&#xff01;专科生毕业论文必备&#xff01;8款一键生成论文工具TOP8测评 2026年专科生毕业论文工具测评&#xff1a;从功能到体验的深度解析 随着人工智能技术的不断进步&#xff0c;越来越多的专科生开始借助AI工具辅助完成毕业论文写作。然而&#xff0c;面对市场上…

作者头像 李华
网站建设 2026/4/18 7:54:52

‍优必选Walker S2机器人2790专利-万祥军| 国研智库·中国国政研究

‍优必选Walker S2机器人2790专利-万祥军| 国研智库中国国政研究在深圳机器人谷的调研现场&#xff0c;国务院总理驻足观看的一幕成为科技界的标志性画面——优必选Walker S2机器人流畅完成自主换电与精密搬运的演示&#xff0c;其关节电机传出的细微嗡鸣声仿佛中国智能制造的新…

作者头像 李华