React(4)事件处理

r囧r小猫 2022-05-31 08:08 351阅读 0赞

10、事件处理

事件的 this

注意,事件触发的 this,默认指向的 undefined;

所以请手动绑定 this 给事件相应函数。比如:

  1. this.clickCount = this.clickCount.bind(this)

onChange 事件

输入框获取修改后的值,通过 onChange 事件。

假如事件的参数是 e,那么 e.target 获取到当前 DOM(即这个 <input> 标签),然后 e.target.value 获取输入框的值。

但这个时候修改是无效的,因此必须通过 this.setState() 来修改值。

onClick事件

绑定点击事件,通过 onClick 事件。

参数同上,一个道理,但这里不需要。

其他事件

略,事件名和原理是一样的

  1. class HelloWord extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. // 通过 state 设置组件变量
  5. this.state = {
  6. count: 0,
  7. bindValue: ''
  8. }
  9. // 在这里手动绑定 this,原因是不绑定的话,this 将指向 undefined
  10. this.clickCount = this.clickCount.bind(this)
  11. this.changeValue = this.changeValue.bind(this)
  12. }
  13. // 渲染函数,this 指向实例本身
  14. render() {
  15. return <div>
  16. {
  17. /* onClick 注意是驼峰写法 */}
  18. <button onClick={
  19. this.clickCount}>点击我增加一次点击计数</button>
  20. <p>你已经点击了{
  21. this.state.count}次</p>
  22. 这个输入框的值和上面的点击次数绑定了,因此无法被手动修改<input type="text" value={
  23. this.state.count}/>
  24. {
  25. /* 下面这个br标签,必须是闭合标签写法,否则会报错 */}
  26. <br/>
  27. <input type="text" value={
  28. this.state.bindValue} placeholder='请在这里输入值' onChange={
  29. this.changeValue}/>
  30. <br/>
  31. 上面输入框的值是:{
  32. this.state.bindValue}
  33. </div>
  34. }
  35. clickCount() {
  36. this.setState({
  37. count: this.state.count + 1
  38. })
  39. }
  40. changeValue(e) {
  41. // e.target 拿到 输入框这个DOM,然后value属性拿到修改后的值
  42. var newValue = e.target.value
  43. console.log(newValue)
  44. // 需要通过 setState 来修改值才能生效
  45. this.setState({
  46. bindValue: newValue
  47. })
  48. }
  49. }

阻止默认事件:

  1. 阻止默认事件,需要通过 e.preventDefaul() 来实现(e 是事件的回调函数的参数)

React 事件函数的特点:

  1. 事件参数是一个合成事件。React 根据 W3C spec 来定义这些合成事件,所以你不需要担心跨浏览器的兼容性问题;
  2. 在 render 里,写成 this.xx,但是这个事件执行时的 this 是 undefined,所以需要手动绑定(bind);

事件的传参:

  1. 原则上,就是返回一个带参数的函数;

【方法一】

返回通过 bind 绑定了 this 和 参数的函数;

需要注意的是,事件参数无需添加,会被默认后置到最后一个参数的位置:

  1. class HelloWord extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = {
  5. count: 0
  6. }
  7. }
  8. // 渲染函数,this 指向实例本身
  9. render() {
  10. return <div>
  11. {
  12. /* 这种方法省略了 this 绑定的过程 */}
  13. <button onClick={
  14. this.clickCount.bind(this, 5)}>增加count</button>
  15. <br/>
  16. 计数器二:{
  17. this.state.count}
  18. </div>
  19. }
  20. clickCount(number, e) {
  21. // 先是自定义参数,最后一个是事件参数
  22. console.log(arguments)
  23. this.setState({
  24. count: this.state.count + number
  25. })
  26. }
  27. }

【方法二】

参数是一个函数,这个函数里执行了你准备执行的那个函数。

核心思想是:参数函数被执行 ——> 参数函数里执行了原本预期执行的函数 ——> 预期执行的函数里,放置了需要的参数

如代码:

  1. class HelloWord extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = {
  5. count: 0
  6. }
  7. }
  8. // 渲染函数,this 指向实例本身
  9. render() {
  10. return <div>
  11. {
  12. /* 这种方法省略了 this 绑定的过程 */}
  13. <button onClick={e => this.clickCount.call(this, 5, e)}>增加count</button>
  14. <br/>
  15. 计数器二:{
  16. this.state.count}
  17. </div>
  18. }
  19. clickCount(number, e) {
  20. // 先是自定义参数,最后一个是事件参数
  21. console.log(arguments)
  22. this.setState({
  23. count: this.state.count + number
  24. })
  25. }
  26. }

发表评论

表情:
评论列表 (有 0 条评论,351人围观)

还没有评论,来说两句吧...

相关阅读