React中this指向丢失问题? 解决方法(经典面试题)

本文详细分析了在React类组件中,this在事件处理函数中为何会指向undefined,以及在严格模式下函数中this的指向。并提出了四种解决方案:使用箭头函数、传递参数的箭头函数、使用bind以及在构造函数中bind。每种方案都提供了相应的代码示例,帮助开发者理解并解决这个问题。
摘要由CSDN通过智能技术生成

问题分析:

this指向undefined问题出现在:
①class组件中 ②在事件处理函数中

​ 1)this不会指向事件源对象—JSX=>React.createElement()=>创建的都是虚拟DOM对象

​ 2)this也不止像window—JSX=>‘use strict’,严格模式下函数中的this不指向window

​ 3)严格模式下,全局函数中的this永远指向undefined

import React, { Component } from 'react'
export default class App extends Component {
  buyCount = 3
   f1() {  //等价于f1=function(){}
     console.log(this); //普通function中的this指向他们的调用者
  	 }
  render() {
    return (
      <>
        <button onClick={this.f1}>-</button>
        <input defaultValue={this.buyCount} />
        <button>+</button>
​      </>
​    )
  }
}

解决方案:

方案1:箭头函数(无法传参数)

f1=()={console.log(this)}

onClick={this.f1}

import React, { Component } from 'react'
export default class App extends Component {
  buyCount = 3
  f1 = () => {
    console.log(this);   //箭头函数不会修改this的指向,而会保留
  }
  render() {
    return (
      <>
        <button onClick={this.f1}>-</button>
        <input defaultValue={this.buyCount} />
        <button>+</button>
      </>
    )
  }
}

方案2:箭头函数(可以传参)

f1(形参){ console.log(this) }

onClick={ ()=>this.f1(实参) }

import React, { Component } from 'react'
export default class App extends Component {
  buyCount = 3
  f1(num) {  //等价于f1=function(){}
    console.log(num, this); //普通function中的this指向他们的调用者
  }
  render() {
    return (
      <>
        <button onClick={() => this.f1(888)}>-</button>
        <input defaultValue={this.buyCount} />
        <button>+</button>

      </>
    )
  }
}

方案3:使用bind

f1=()={console.log(this)}

onClick={this.f1.bind(this)}

import React, { Component } from 'react'
export default class App extends Component {
  buyCount = 3
  f1() {  //等价于f1=function(){}
    console.log(this); //普通function中的this指向他们的调用者
  }
  render() {
    return (
      <>
        <button onClick={this.f1.bind(this)}>-</button>
        <input defaultValue={this.buyCount} />
        <button>+</button>
      </>
    )
  }
}

方案4: 使用bind

constructor(){

​ super()

​ this.f1=this.f1.bind(this)

}

f1(){ log(this)}

onClick={this.f1}

import React, { Component } from 'react'
export default class App extends Component {
  buyCount = 3
  constructor() {
    // 子类构造方法第一句话永远是
    super() //调用父类的构造方法,在子类体内先构建一个父类对象
    console.log('一个app类型的对象被创建');
    // this.buyCount = 8 //构造方法内可以对数据重赋值
    this.f1 = this.f1.bind(this)
  }
  f1() {  //等价于f1=function(){}
    console.log(this); //普通function中的this指向他们的调用者
  }
  render() {
    return (
      <>
        <button onClick={this.f1}>-</button>
        <input defaultValue={this.buyCount} />
        <button onClick={this.f1}>+</button>
      </>
    )
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

捂耳听枫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值