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

React -TS学习—— useRef

1、获取dom

2、稳定引用的存储器(定时器管理)

import { useEffect, useRef } from "react";
// 1、获取dom
// 2、稳定引用的存储器(定时器管理)
function App() {const domRef =  useRef<HTMLInputElement>(null);const timerRef = useRef<number | undefined>(undefined);useEffect(()=>{// current?  可选链  前面不为空值(null 、undefined)执行点运算,防止出现空值点运算错误domRef.current?.focus();// 定时器timerRef.current = setInterval(()=>{console.log('定时器');},1000)return ()=>{clearInterval(timerRef.current);}},[])const stopTimer = () => {if (timerRef.current !== undefined) {clearInterval(timerRef.current);timerRef.current = undefined; // 防止内存泄漏}};return (<><input type="text" /><button onClick={stopTimer}>停止定时器</button></>)
}
export default App


http://www.mrgr.cn/news/13713.html

相关文章:

  • Swift concurrency 4 — Task和.task的理解与使用
  • Go 服务调试精解
  • ZooKeeper体系架构、安装、HA
  • [MySql]保姆级上手教程
  • 软考:软件设计师 — 17.程序设计语言与语言处理程序基础
  • Datawhale X 李宏毅苹果书 AI夏令营_深度学习基础学习心得Task2
  • SSM复习
  • 前端速通面经八股系列(五)—— Vue(上)
  • 语义分割训练精度计算
  • python基础(12迭代器生成器)
  • kotlin中常用扩展函数
  • 在VB.net中,LINQ在数据统计方面的应用,举例说明
  • 数据结构与算法学习day18-层序遍历
  • Python-MNE-源定位和逆问题01:源估计(SourceEstimate)数据结构
  • Linux学习笔记4 重点!网络排障命令
  • Go 中间件学习
  • Spark MLlib 特征工程系列—特征转换VectorSizeHint
  • 扑捉一只耿鬼(HTML文件)
  • 在Ubuntu 18.04上如何从默认的APT仓库安装MongoDB
  • 【Yarn】Yarn的基本执行流程(二)AM Container的启动