react hooks 计数器实现代码
更新时间:2023年08月24日 17:15:27 作者:徐同保
这篇文章主要介绍了react hooks计数器实现代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
今天给大家分享react hooks 计数器实现代码,代码如下所示:
180秒倒计时
const [count, setCount] = useState(0) setCount(180) useEffect(() => { clearTimeout(timer) timer = setTimeout(() => { if (count > 1) { setCount(count - 1) } else { setIsSendEmail(false) } }, 1000) // eslint-disable-next-line }, [count])
import { useState, useRef, useEffect } from 'react' import Api from '../../../api' import Toast from 'react-native-root-toast' import { checkEmail } from '../../../utils/tools' import AsyncStorage from '@react-native-async-storage/async-storage' let timer export default function useList(props) { const [username, setUsername] = useState( '' ) const [emailCode, setEmailCode] = useState( '' ) const [isSendEmail, setIsSendEmail] = useState(false) const [count, setCount] = useState(0) const [emailId, setEmailId] = useState('') const [avatar, setAvatar] = useState(null) const [nickname, setNickname] = useState( '' ) const [password, setPassword] = useState( '' ) const [code, setCode] = useState( '' ) const [visible, setVisible] = useState(false) // eslint-disable-next-line const [isLoading, setIsLoading] = useState(false) const [captchaId, setCaptchaId] = useState('') const [captcha, setCaptcha] = useState('') const [visible1, setVisible1] = useState(false) const usernameEl = useRef(null) const { navigation } = props const toggleDialog1 = () => { setVisible1(!visible1) } const handleInput = (e) => { setUsername(e) } const handleSendEmail = () => { const { isEmail, message } = checkEmail(username) if (username.trim() === '') { Toast.show('邮箱不能为空', { duration: 3000, position: Toast.positions.CENTER, }) return } else if (isEmail === false) { Toast.show(message, { duration: 3000, position: Toast.positions.CENTER, }) return } Api.h5.userSendEmailCode({ username }).then((res) => { if (res.code === 200) { setEmailId(res.data.emailId) Toast.show(res.message, { duration: 3000, position: Toast.positions.CENTER, }) setCount(180) setIsSendEmail(true) } }) } const handleRegister = () => { const { isEmail, message } = checkEmail(username) if (username.trim() === '') { Toast.show('邮箱不能为空', { duration: 3000, position: Toast.positions.CENTER, }) return } else if (isEmail === false) { Toast.show(message, { duration: 3000, position: Toast.positions.CENTER, }) return } else if (password.trim() === '') { Toast.show('密码不能为空', { duration: 3000, position: Toast.positions.CENTER, }) return } else if (code.trim() === '') { Toast.show('验证码不能为空', { duration: 3000, position: Toast.positions.CENTER, }) return } else if (!emailId) { Toast.show('请获取邮箱验证码', { duration: 3000, position: Toast.positions.CENTER, }) return } Api.h5 .userAiAdd({ username, emailCode, emailId, avatar, nickname, password, code, captchaId, }) .then((res) => { if (res.code === 200) { navigation.navigate('Login') } }) } const handleGuest = () => { Api.h5.userAiGuestAdd({}, false).then((res) => { if (res.code === 200) { const { username, password } = res.data Api.h5 .userAiLogin({ username, password, isGuest: true }, false) .then(async (res) => { if (res.code === 200) { const { username, nickname, token, talkId, uid } = res.data AsyncStorage.setItem('username', username) await AsyncStorage.setItem('nickname', nickname) AsyncStorage.setItem('token', token) AsyncStorage.setItem('talkId', talkId) AsyncStorage.setItem('uid', uid) navigation.navigate('Index', { type: 'home', }) } }) } }) } const handleNav = (path) => { navigation.navigate(path) } const handleVisilbe = () => { setVisible(!visible) } const getCaptcha = async () => { Api.h5.userCaptcha({}).then((res) => { if (res.code === 200) { const { captchaId, captcha } = res.data let svg = captcha let height = svg.indexOf('height') let width = svg.indexOf('width') let step1 = svg.slice(0, height + 8) let step2 = svg.slice(height + 8 + 2) svg = `${step1}150${step2}` let step3 = svg.slice(0, width + 5) let step4 = svg.slice(width + 8 + 3) svg = `${step3}450${step4}` let html = `<div style="text-align:center;width:100%;overflow:hidden;">${svg}</div>` setCaptcha(html) setCaptchaId(captchaId) } }) } const handleUploadFileCallback = (key) => { setAvatar(key) } useEffect(() => { getCaptcha() }, []) useEffect(() => { clearTimeout(timer) timer = setTimeout(() => { if (count > 1) { setCount(count - 1) } else { setIsSendEmail(false) } }, 1000) // eslint-disable-next-line }, [count]) useEffect(() => { Api.h5.uploadGetTokenForH5().then(async (res) => { if (res.code === 200) { await AsyncStorage.setItem('qiniuUploadTokenForH5', res.data.token) } }) }, []) return { username, emailCode, isSendEmail, count, avatar, nickname, password, code, visible, isLoading, captcha, visible1, usernameEl, setEmailCode, handleSendEmail, setNickname, setPassword, setCode, toggleDialog1, handleInput, handleRegister, handleGuest, handleNav, handleVisilbe, getCaptcha, handleUploadFileCallback, } }
到此这篇关于react hooks 计数器实现代码的文章就介绍到这了,更多相关react hooks 计数器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
React路由的history对象的插件history的使用解读
这篇文章主要介绍了React路由的history对象的插件history的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-10-10再次谈论React.js实现原生js拖拽效果引起的一系列问题
React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript MVC 框架,都不满意,就决定自己写一套,用来架设 Instagram 的网站.本文给大家介绍React.js实现原生js拖拽效果,需要的朋友一起学习吧2016-04-04React工作流程及Error Boundaries实现过程讲解
这篇文章主要介绍了React工作流程及Error Boundaries实现过程讲解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-02-02ForwardRef useImperativeHandle方法demo
这篇文章主要为大家介绍了ForwardRef useImperativeHandle方法demo,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-03-03
最新评论