- 通过onXxxx属性指定事件处理函数(注意大小写)
- React使用的是自定义(合成)事件, 而不是使用原生的DOM事件——为了更好的兼容性。
- React中的事件是通过事件委托方式处理的(委托给组件最外层的元素)——为了高效
- 通过event.target得到发生事件的DOM元素对象——不要过多的使用ref
import React from 'react'class App extends React.Component {inputRef1 = React.createRef()showData = () => {alert(this.inputRef1.current.value)}shouData2 = (e) => {//发生事件的DOM和操作事件的DOM是同一个alert(e.target.value)}render() {return (<div><input ref = {this.inputRef1} placeholder="点击提示数据" type='text'/><button type="" onClick={this.showData}>点击提示左侧的数据</button><input onBlur={this.shouData2} type="text" placeholder='失去焦点提示右侧数据'/></div>);}}export default App;
