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> ); } }