事件是我们常用的处理用户交互的方式,那么在react里面如何处理事件呢?react里面处理事件的固定格式是
语法:
<element on事件={处理函数}>some thing</element>
例子:
<button onClick={()=>{console.log('点击事件')}}>按钮</button>
语法:
import React, { Component } from "react";
export default class MyClickBind extends Component {
//定义事件
EventHandlers(){
console.log('函数触发');
}
render() {
return {
//触发事件
<buttom onClick={EventHandlers()}>点击触发事件</buttom>
}
}
}
但是我们通常需要将处理函数写到render函数的外面,因为我们的事件处理函数里面的代码可能非常复杂,直接写在jsx里面会非常难以维护。