React事件监听的代码怎么写
发表于:2024-10-25 作者:千家信息网编辑
千家信息网最后更新 2024年10月25日,这篇文章主要介绍"React事件监听的代码怎么写"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"React事件监听的代码怎么写"文章能帮助大家解决问题。方法一:
千家信息网最后更新 2024年10月25日React事件监听的代码怎么写
这篇文章主要介绍"React事件监听的代码怎么写"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"React事件监听的代码怎么写"文章能帮助大家解决问题。
方法一:在constructor
中使用bind
绑定,改变this
的指向,代码如下:
import React, { Component } from 'react';export default class Group extends Component { constructor(props) { super(props); this.state = { show: true, title: '大西瓜' }; // 写法一:事件绑定改变this指向 this.showFunc = this.showFunc.bind(this); } // 调用该方法 showFunc() { this.setState({ show: false }); } render() { let result = this.state.show ? this.state.title : null; return ( {result} ); }}
方法二:通过箭头函数改变this
指向,代码如下:
import React, { Component } from 'react';export default class Group extends Component { constructor(props) { super(props); this.state = { show: true, title: '大西瓜' }; } // 第二种,通过箭头函数改变this指向 showFunc = () => { this.setState({ show: false }); }; render() { let result = this.state.show ? this.state.title : null; return ( {result} ); }}
方法三:直接使用箭头函数改变this
的指向,代码如下:
import React, { Component } from 'react';export default class Group extends Component { constructor(props) { super(props); this.state = { show: true, title: '大西瓜' }; } // 调用该方法 showFunc() { this.setState({ show: false }); } render() { let result = this.state.show ? this.state.title : null; return ( {result} ); }}
关于"React事件监听的代码怎么写"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。
代码
方法
指向
事件
监听
函数
知识
箭头
西瓜
行业
不同
实用
内容
写法
实用性
实际
文章
更多
案例
知识点
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器安全设置下载文件
网络安全和信息化龙头股
掌握网络安全技术
日志插入数据库
武威市工会网络安全知识竞赛
数据库进销存教程
网络安全审计6
软件开发qt
sql 数据库表增加一行
服务器主板卡68
数据库中建物理模型的工具
软件开发既征既退
数据库服务攻击
手机百度如何连接服务器
大学课件软件开发
waves服务器固件下载
做服务器管理的软件
广州职友集网络技术
十点风云及网络技术有限公司
网络服务人员网络安全意识
手机m3怎么设置服务器
河北果蔬软件开发哪家专业
obs 服务器
dpl数据库中dpl是什么意思
软件开发商品进货链接
登录服务器管理员
数据库怎么清理游戏背包
天创数码集团 软件开发
用友不能初始化数据库
吉林软件开发自学课程