码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • react-通过useRef完成倒计时60秒发送验证码效果


    文章目录

    • 1.useRef属性
    • 2.useRef的深度用法
    • 3.发送验证码倒计时60秒:

    1.useRef属性

    • 导入 useRef 函数从 react 中
    • 创建ref对象 const ref = useRef(null)
    • 给需要获取的标签上 ref={ref} 绑定ref对象
    • 渲染完毕后,可以通过 ref.current 获取dom元素
    // 测试 自定义hook
    import { usePoint } from "./utils/hooks"
    import { useRef } from "react" //1.
    const App = () => {
      const point = usePoint()
      const inputRef = useRef(null) // null表示初始值  inputRef.current 初始值为null // 2.
      const getInputValue = () => {
        // 4.
        console.log(inputRef.current.value)
      }
      return (
        <div>
          <p>
            当前的鼠标的坐标是{point.pageX}, {point.pageY}
          </p>
          {/* 3. */}
          <input ref={inputRef} type="text" />
          <button onClick={() => getInputValue()}>获取dom对象</button>
        </div>
      )
    }
    export default App
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    2.useRef的深度用法

    在这里插入图片描述

    • 不论组件如何更新,ref的存储值都不会发生变化,除非你设置它
    • useState中的状态 在函数中永远都是最新的状态

    如:如何来获取上一次的状态

    // 获取上一次的状态
    
    export const usePrevStatus = (state) => {
      const valueRef = useRef(state) // 生成一个ref对象
      useEffect(() => {
        valueRef.current = state // 赋值最新的值
      }, [state])
    
      return valueRef.current // 返回这个值
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    使用:

      const [count, setCount] = useState(0)
      const prevValue = usePrevStatus(count)
        console.log(prevValue)
    
    • 1
    • 2
    • 3

    3.发送验证码倒计时60秒:

    效果展示:
    在这里插入图片描述
    在这里插入图片描述
    代码:

    import "./App.css"
    import { Button } from "antd"
    import { useState, useRef, useEffect } from "react"
    const App = () => {
      const timerCount = 60 // 默认60秒
      const [count, setCount] = useState(timerCount)
      const timerRef = useRef(null) // 记录时间的定时器
      const cutCount = () => {
        setCount((prevState) => prevState - 1) // 为什么这里要用函数- 如果用count 发现count闭包了 不会发生变化了
      }
      const sendCode = () => {
        // 要发送验证码
        cutCount()
        timerRef.current = setInterval(cutCount, 1000)
      }
      useEffect(() => {
        if (count === 0) {
          clearInterval(timerRef.current) // 清空定时器
          setCount(timerCount) // 重新将技术器设置为60秒
        }
      }, [count])
    
      return (
        <div className="App">
          <Button
            type="primary"
            disabled={count < timerCount}
            onClick={count === timerCount ? sendCode : null}
          >
            {count === timerCount ? "发送验证码" : `还剩${count}秒`}
          </Button>
        </div>
      )
    }
    
    export default App
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
  • 相关阅读:
    Windows成员服务器用户权利/本地策略审计
    upload-labs通关
    MySql索引详解-各种索引的定义与区别和应用
    12月2日第壹简报,星期五,农历十一月初九
    怒刷LeetCode的第22天(Java版)
    【剑指Offer】8.二叉树的下一个结点
    【Leetcode】剑指Offer 18:删除链表的节点
    文心一言 VS 讯飞星火 VS chatgpt (121)-- 算法导论10.4 2题
    sheng的学习笔记-【中文】【吴恩达课后测验】Course 2 - 改善深层神经网络 - 第一周测验
    零代码编程:用ChatGPT批量采集bookroo网页上的英文书目列表
  • 原文地址:https://blog.csdn.net/m0_62181310/article/details/126666404
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号