React?Native?中實(shí)現(xiàn)倒計(jì)時(shí)功能
正文
在 React Native,該如何實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)功能呢?

首次實(shí)現(xiàn)
表面看來很簡(jiǎn)單,譬如像下面這樣:
const timer = useRef<ReturnType<typeof setInterval> | null>(null)
const [count, setCount] = useState(0)
const start = () => {
setCount(10)
timer.current = setInterval(() => {
setCount((count) => count - 1)
}, 1000)
}
useEffect(() => {
if (count === 0 && timer.current !== null) {
clearInterval(timer.current)
timer.current = null
}
}, [count])
這段代碼大多數(shù)情況下是可以正常工作的。但是你將應(yīng)用退到后臺(tái),稍后再進(jìn)入看看。
很有可能,原本應(yīng)該結(jié)束的倒計(jì)時(shí),還在工作。
這是因?yàn)?React Native 應(yīng)用退到后臺(tái)后,世界會(huì)停止。為了適應(yīng)這點(diǎn),我們應(yīng)該先設(shè)定希望倒計(jì)時(shí)結(jié)束的時(shí)間,然后每隔一秒計(jì)算一次當(dāng)前時(shí)間與結(jié)束時(shí)間之差(秒)。
此外,當(dāng)應(yīng)用退到后臺(tái)時(shí),應(yīng)該清除定時(shí)器。
最終實(shí)現(xiàn)
考慮上述種種,倒計(jì)時(shí)的實(shí)現(xiàn)并不簡(jiǎn)單。
我們可以封裝一個(gè)自定義 Hook 來提供可復(fù)用的倒計(jì)時(shí)功能。
import { useAppState } from '@react-native-community/hooks'
import { useCallback, useEffect, useRef, useState } from 'react'
export function useCountdown(seconds = 30) {
const timer = useRef<ReturnType<typeof setInterval> | null>(null)
const [target, setTarget] = useState<Date | null>(null)
const [count, setCount] = useState<number>(0)
const appState = useAppState()
const start = useCallback(() => {
setTarget(add(new Date(), seconds))
}, [seconds])
const stop = useCallback(() => {
setTarget(null)
setCount(0)
}, [])
useEffect(() => {
if (target === null || appState !== 'active') {
return
}
setCount(diff(new Date(), target))
timer.current = setInterval(() => {
setCount(diff(new Date(), target))
}, 1000)
return () => {
if (timer.current) {
clearInterval(timer.current)
timer.current = null
}
}
}, [target, appState])
useEffect(() => {
if (count === 0) {
stop()
}
}, [count, stop])
return { count, start, stop }
}
function add(date: Date, seconds: number) {
return new Date(date.getTime() + seconds * 1000)
}
function diff(now: Date, target: Date) {
return Math.max(
Math.trunc((target.getTime() - now.getTime()) / 1000 + 0.5),
0
)
}
示例
這里有一個(gè)示例,供你參考。
以上就是React Native 中實(shí)現(xiàn)倒計(jì)時(shí)功能的詳細(xì)內(nèi)容,更多關(guān)于React Native倒計(jì)時(shí)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
ahooks useVirtualList 封裝虛擬滾動(dòng)列表
這篇文章主要為大家介紹了ahooks useVirtualList 封裝虛擬滾動(dòng)列表詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
從頭寫React-like框架的工程搭建實(shí)現(xiàn)
這篇文章主要介紹了從頭寫React-like框架的工程搭建實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
詳解React如何使用??useReducer???高階鉤子來管理狀態(tài)
useReducer是React中的一個(gè)鉤子,用于替代?useState來管理復(fù)雜的狀態(tài)邏輯,本文將詳細(xì)介紹如何在React中使用?useReducer高階鉤子來管理狀態(tài),感興趣的可以了解下2025-02-02
React+Spring實(shí)現(xiàn)跨域問題的完美解決方法
這篇文章主要介紹了React+Spring實(shí)現(xiàn)跨域問題的完美解決方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
淺談react.js中實(shí)現(xiàn)tab吸頂效果的問題
下面小編就為大家?guī)硪黄獪\談react.js中實(shí)現(xiàn)tab吸頂效果的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09

