React hooks - 自定义hooks

embedded/2024/9/25 17:19:58/

自定义hooks

        • 自定义封装鼠标位置的 hook
        • 自定义封装秒数倒计时的 hook

在 src 目录下新建 hooks/index.ts 模块,自定义hooks都写在这里,自定义hooks都以use开头

自定义封装鼠标位置的 hook
export const useMousePosition = (delay:number = 0) => {const [position, setPosition] = useState({ x: 0, y: 0 }) // 鼠标的位置useEffect(() => {let timeId : null | NodeJS.Timeout = nullconst mouseMoveHandler = (e: MouseEvent) => {if(timeId !== null) return timeId = setTimeout(() => {console.log({ x: e.clientX, y: e.clientY })setPosition({ x: e.clientX, y: e.clientY })timeId = null }, delay)}window.addEventListener('mousemove', mouseMoveHandler)return () => window.removeEventListener('mousemove', mouseMoveHandler)}, [])return position // 返回鼠标的位置
}
import { useMousePosition } from '@/hooks/index'
const MouseInfo: React.FC = () => {const position = useMousePosition(200) // 调用自定义hooks获取位置,还可以传递参数return (<><p>鼠标的位置:{JSON.stringify(position)}</p></>)
}
自定义封装秒数倒计时的 hook
// 设置hooks函数的类型,num后面加一个?代表可传可不穿,不穿默认为10
type useCountDown = (num?: number) => [number, boolean] 
export const useCountDown: useCountDown = (num: number = 10) => {const seconds = Math.round(Math.abs(num)) || 10 // 对边界值进行处理,取绝对值再取整,如果传入为0默认设置为10const [count, setCount] = useState(seconds) const [disabled, setDisabled] = useState(true) // useEffect(() => {//  let intervalId : null | NodeJS.Interval = null//  intervalId = setInterval(() => {//     if(count>1) setCount(prev=>prev-1)//     else {//       clearInterval(intervalId)//       setDisabled(false)//     }//  }, 1000)//  return ()=>clearInterval(intervalId)// }, [])useEffect(() => {let timeId : null | NodeJS.Timeout = nulltimeId = setTimeout(() => {if(count>1) setCount(prev=>prev-1)else {clearTimeout(intervalId)setDisabled(false)}}, 1000)return ()=>clearTimeout(intervalId)}, [count])return [restCount,disabled] // 返回鼠标的位置
}
import { useCountDown } from '@/hooks/index'
const MouseInfo: React.FC = () => {const [count,disabled] = useCountDown(5) return (<><button disabled={disabled}>{disabled?`请仔细阅读本协议(${count})秒`:'请确认协议'}</button></>)
}

http://www.ppmy.cn/embedded/43301.html

相关文章

从华为云Redis到AWS ElastiCache的操作方法

越来越多企业选择出海&#xff0c;那么就涉及到IT系统的迁移&#xff0c;本文将详细介绍如何将华为云Redis顺利迁移到AWS ElastiCache的操作方法&#xff0c;九河云将为您介绍迁移步骤以帮助您顺利完成这一重要任务。 **1. 确定迁移计划** 在开始迁移之前&#xff0c;首先要制…

找到字符串中所有字母异位词-力扣

首先想到的解法时利用滑动窗口&#xff0c;每次匹配一个长度等于p字符串长度的s字符串的子串&#xff0c;然后进行判定&#xff0c;如果是&#xff0c;则将所在位置添加到数组中。 在判断两个字符串是否是字母异位词时&#xff0c;直接copy了之前的代码&#xff0c;整体代码如下…

【OpenHarmony V4.1.1 源码解析 - 000】文章链接汇总

【OpenHarmony V4.1.1 源码解析 - 000】文章链接汇总 Release Note 链接&#xff1a; 《OpenHarmony-v4.1.1-release.md》 《Release-Note》源码下载链接&#xff1a; 《OpenHarmony-v4.1.1-Release.tar.gz》编译环境配置&#xff1a; 《Docker编译环境》

springMVC,springboot整合jasypt

生成加密串 public class Encryptor { public static void main(String[] args) { BasicTextEncryptor textEncryptor new BasicTextEncryptor(); textEncryptor.setPassword("mysalt");//自定义加密盐 String myEncryptedPassword t…

浅谈Docker容器的网络通信原理

文章目录 1、回顾容器概念2、容器网络3、容器与主机之间的网络连通4、交换机的虚拟实现---虚拟网桥&#xff08;Bridge&#xff09;5、Docker 守护进程daemon管理容器网络 1、回顾容器概念 我们知道容器允许我们在同一台宿主机&#xff08;电脑&#xff09;上运行多个服务&…

HTML静态网页成品作业(HTML+CSS)——宠物狗介绍网页(3个页面)

&#x1f389;不定期分享源码&#xff0c;关注不丢失哦 文章目录 一、作品介绍二、作品演示三、代码目录四、网站代码HTML部分代码 五、源码获取 一、作品介绍 &#x1f3f7;️本套采用HTMLCSS&#xff0c;未使用Javacsript代码&#xff0c;共有3个页面。 二、作品演示 三、代…

Golang框架HTTP客户端框架zdpgo_resty发送表单请求

核心代码 这里通过字典传递了一个简单的表单数据。 发送的是POST请求。 resp, err : client.R().SetFormData(map[string]string{"username": "jeeva","password": "mypass",}).Post("http://127.0.0.1:3333/login")fmt.P…

安徽京准、子母钟系统(网络时钟系统)在机场应用方案

安徽京准、子母钟系统&#xff08;网络时钟系统&#xff09;在机场应用方案 安徽京准、子母钟系统&#xff08;网络时钟系统&#xff09;在机场应用方案 摘要&#xff1a;某机场指挥调度、离港系统、航显广播等多个重要信息系统之间的时钟同步&#xff0c;对机场的正常运营和安…