当前位置: 首页 > news >正文

react当中的this指向

绑定this常见的方法

在JavaScript当中,this的指向取决于函数调用的上下文,但是在react当中,this通常指向指向问题是一个常见的困扰,如果this没有正确绑定,那么方法中的this很可能会是undefined,导致程序出错

 1.bind(this):

在构造函数当中使用.bind(this)来this的指向绑定,这个方法比较方便,绑定一次即可生效,性能比较好,但是代码比较长

Test1ConstructorBind 继承自React.Component,这是定义 React 类组件的标准方式。

在构造函数中使用this.method.bind(this)为每个事件处理方法绑定this,确保方法内部的this指向组件实例。

import React, { Component } from 'react'class Test1ConstructorBind extends Component {constructor(props) {super(props)this.state = {message: 'Hello!',count: 0,isVisible: true,}this.handleClick = this.handleClick.bind(this)this.handleCount = this.handleCount.bind(this)this.toggleVisibility = this.toggleVisibility.bind(this)this.handleReset = this.handleReset.bind(this)}//切换消息文本handleClick() {console.log('方法1 - 构造函数绑定: 当前消息:', this.state.message)this.setState({message: this.state.message === 'Hello!' ? '你好!' : 'Hello!',})}//增加计数handleCount() {this.setState((prevState) => ({count: prevState.count + 1,}))}//切换内容显示 / 隐藏toggleVisibility() {this.setState((prevState) => ({isVisible: !prevState.isVisible,}))}//重置所有状态handleReset() {this.setState({count: 0,message: 'Hello!',isVisible: true,})}render() {return (<div><h2>方法1: 构造函数中绑定 this</h2><p>在 constructor 中使用 this.method = this.method.bind(this) 绑定</p><button onClick={this.handleClick}>切换消息</button><span> 当前消息: {this.state.message}</span><br /><button onClick={this.handleCount}>增加计数</button><span> 计数: {this.state.count}</span><br /><button onClick={this.toggleVisibility}>{this.state.isVisible ? '隐藏' : '显示'}内容</button>{this.state.isVisible && <div>这是可以切换显示/隐藏的内容</div>}<br /><button onClick={this.handleReset}>重置所有状态</button></div>)}
}export default Test1ConstructorBind

2.使用箭头函数:

箭头函数不绑定自己的this,它会捕获所在上下文的this值,使用这种方法无需手动绑定,每次渲染的时候需要创建两个函数实例,可能会影响性能

import React, { Component } from 'react'class Test2ArrowFunction extends Component {constructor(props) {super(props)this.state = {message: 'Hello!',count: 0,isVisible: true,}// 使用箭头函数时,不需要在构造函数中绑定}// 方法2: 使用箭头函数自动绑定thishandleClick = () => {console.log('方法2 - 箭头函数: 当前消息:', this.state.message)this.setState({message: this.state.message === 'Hello!' ? '你好!' : 'Hello!',})}handleCount = () => {this.setState((prevState) => ({count: prevState.count + 1,}))}toggleVisibility = () => {this.setState((prevState) => ({isVisible: !prevState.isVisible,}))}handleReset = () => {this.setState({count: 0,message: 'Hello!',isVisible: true,})}render() {return (<div><h2>方法2: 箭头函数自动绑定 this</h2><p>使用箭头函数语法,this 自动绑定到组件实例</p><button onClick={this.handleClick}>切换消息</button><span> 当前消息: {this.state.message}</span><br /><button onClick={this.handleCount}>增加计数</button><span> 计数: {this.state.count}</span><br /><button onClick={this.toggleVisibility}>{this.state.isVisible ? '隐藏' : '显示'}内容</button>{this.state.isVisible && <div>这是可以切换显示/隐藏的内容</div>}<br /><button onClick={this.handleReset}>重置所有状态</button></div>)}
}export default Test2ArrowFunction

3.使用内联bind:

组件较小且方法简单,需要快速传递参数,方法只在一处使用,无需复用。

大型列表渲染,性能敏感的组件,方法需要被多次复用(如传递给子组件)。

import React, { Component } from 'react'class Test4InlineBind extends Component {constructor(props) {super(props)this.state = {message: 'Hello!',count: 0,isVisible: true,}}handleClick() {console.log('方法4 - 内联bind: 当前消息:', this.state.message)this.setState({message: this.state.message === 'Hello!' ? '你好!' : 'Hello!',})}handleCount() {this.setState((prevState) => ({count: prevState.count + 1,}))}toggleVisibility() {this.setState((prevState) => ({isVisible: !prevState.isVisible,}))}handleReset() {this.setState({count: 0,message: 'Hello!',isVisible: true,})}render() {return (<div><h2>方法4: 内联 bind 方法</h2><p>在 render 中使用 onClick=this.handleClick.bind(this) 语法(性能较差)</p><button onClick={this.handleClick.bind(this)}>切换消息</button><span> 当前消息: {this.state.message}</span><br /><button onClick={this.handleCount.bind(this)}>增加计数</button><span> 计数: {this.state.count}</span><br /><button onClick={this.toggleVisibility.bind(this)}>{this.state.isVisible ? '隐藏' : '显示'}内容</button>{this.state.isVisible && <div>这是可以切换显示/隐藏的内容</div>}<br /><button onClick={this.handleReset.bind(this)}>重置所有状态</button><div><small>注意:这种方法每次渲染都会创建新的函数,影响性能,不推荐在生产环境中使用。</small></div></div>)}
}export default Test4InlineBind

4.使用内联箭头函数:

语法简洁,自动绑定this,可直接传递参数

每次渲染都会创建新的函数实例,可能影响性能

import React, { Component } from 'react'class Test3InlineArrow extends Component {constructor(props) {super(props)this.state = {message: 'Hello!',count: 0,isVisible: true,}}render() {return (<div><h2>方法3: 内联箭头函数</h2><p>在 render 中使用 onClick={() => this.handleClick()} 语法(性能较差)</p><buttononClick={() => {console.log('方法3 - 内联箭头函数: 当前消息:', this.state.message)this.setState({message: this.state.message === 'Hello!' ? '你好!' : 'Hello!',})}}>切换消息</button><span> 当前消息: {this.state.message}</span><br /><buttononClick={() => {this.setState((prevState) => ({count: prevState.count + 1,}))}}>增加计数</button><span> 计数: {this.state.count}</span><br /><buttononClick={() => {this.setState((prevState) => ({isVisible: !prevState.isVisible,}))}}>{this.state.isVisible ? '隐藏' : '显示'}内容</button>{this.state.isVisible && <div>这是可以切换显示/隐藏的内容</div>}<br /><buttononClick={() => {this.setState({count: 0,message: 'Hello!',isVisible: true,})}}>重置所有状态</button><div><small>注意:这种方法每次渲染都会创建新的函数,影响性能,不推荐在生产环境中使用。</small></div></div>)}
}export default Test3InlineArrow

http://www.xdnf.cn/news/1083871.html

相关文章:

  • 【从0-1的CSS】第3篇:盒子模型与弹性布局
  • 《前端路由重构:解锁多语言交互的底层逻辑》
  • 3. lvgl 9.3 vscode 模拟环境搭建 lv_port_pc_vscode-release-v9.3
  • Paimon索引概述
  • vue3.4中的v-model的用法~
  • 支持向量机(SVM)在肝脏CT/MRI图像分类(肝癌检测)中的应用及实现
  • 从源码到思想:OneCode框架模块化设计如何解决前端大型应用痛点
  • Application的onLowMemory从Android API 34开始系统不再触发,从API 35开始废弃
  • 【BTC】协议(共识机制)
  • 自定义指令
  • java+vue+SpringBoo职业生涯规划系统(程序+数据库+报告+部署教程+答辩指导)
  • 【AI大模型】Spring AI 基于mysql实现对话持久存储详解
  • 多模态大语言模型arxiv论文略读(149)
  • 【网络协议安全】任务13:ACL访问控制列表
  • 深度学习图像分类数据集—蘑菇可食性识别分类
  • 使用Python将PDF转换成word、PPT
  • 量子计算机技术(第二节,到底什么是量子)
  • 【CSS-15】深入理解CSS transition-duration:掌握过渡动画的时长控制
  • 高速信号眼图
  • ASP.NET代码审计 Web Forms框架 SQL注入漏洞
  • 【Python】使用读取到的文件
  • 零成本搭建浏览器远程Linux桌面:Ubuntu/Debian方案全解析
  • MySQL数据库主从复制
  • python-if结构、三目运算符
  • 善用关系网络:开源AI大模型、AI智能名片与S2B2C商城小程序赋能下的成功新路径
  • 知识文档管理系统选型指南(中小企业专用)
  • CppCon 2018 学习:What Do We Mean When We Say Nothing At All?
  • 一文掌握Qt Quick数字图像处理项目开发(基于Qt 6.9 C++和QML,代码开源)
  • 计算机网络1.1:什么是Internet?
  • 电商系统二次开发找谁做?ZKmall开源商城前后端分离技术更易升级迭代