React實(shí)現(xiàn)倒計(jì)時(shí)功能組件
故事背景
倒計(jì)時(shí)(或者時(shí)間顯示)是日常開發(fā)中的常見任務(wù)
常用在權(quán)限控制或者日期顯示上
由于經(jīng)常會(huì)使用到,所以將其分裝成一個(gè)組件,方便以后復(fù)用
項(xiàng)目結(jié)構(gòu)搭建
npx create-react-app test-countdown --template=typescript yarn add antd
倒計(jì)時(shí)組件
1. App.tsx中的內(nèi)容
// App.tsx /* eslint-disable @typescript-eslint/no-useless-constructor */ import React from 'react'; import { Tooltip, Modal, Button } from "antd"; import './App.css'; // 秒轉(zhuǎn)時(shí)間 function convertSecondsToHMS(seconds: number) { const hours = Math.floor(seconds / 3600); const minutes = Math.floor((seconds % 3600) / 60); const remainingSeconds = Math.floor(seconds % 60); const formattedTime = `${hours}小時(shí) ${minutes}分鐘 ${remainingSeconds}秒`; return formattedTime; } // 倒計(jì)時(shí)組件props格式 type IProps = { level: boolean; timeRemain: number; } // 倒計(jì)時(shí)組件state格式 type IState = { level: boolean; timeRemain: number; lastMountedTime: number; timeCountdown: number; timer: any; } // 倒計(jì)時(shí)組件 class App extends React.PureComponent<IProps, IState> { state = { lastMountedTime: 0, timeRemain: 0, timeCountdown: 9999, level: false, timer: null, } // 多余的構(gòu)造函數(shù) constructor(props: IProps) { super(props); } // 組件構(gòu)建完畢之后需要將props中設(shè)置的等級(jí),倒計(jì)時(shí)存到state中去 // 此外還需要將當(dāng)?shù)貢r(shí)間存到state中去作為之后計(jì)算的參考 // 最后開啟定時(shí)器,并將定時(shí)器的handle存到state中去 componentDidMount(): void { const { timeRemain, level } = this.props; const lastMountedTime = +new Date(); const timer = setInterval( () => { this.setState(()=>({ timeCountdown: this.state.timeRemain - (+new Date() - this.state.lastMountedTime) / 1000, })) }, 200 ) this.setState({ lastMountedTime, timeRemain, level, timer, }) } // 在組件卸載之前需要釋放定時(shí)器 componentWillUnmount(): void { if(this.state.timer) clearInterval(this.state.timer); } // 如果用戶是VIP則tooltip上不顯示訪問剩余時(shí)間 // 如果用戶是訪客,則字體變成紅色,tooltip顯示倒計(jì)時(shí) // 如果倒計(jì)時(shí)為0則彈出模態(tài)框通知用戶試用結(jié)束 render() { const info = this.state.level ? 'VIP用戶' : '訪客模式'; const fontColor = this.state.level ? 'black' : 'red'; const timeCountdown = this.state.timeCountdown > 0 ? this.state.timeCountdown : 0; const toolTipInfo = this.state.level ? '' : convertSecondsToHMS(timeCountdown); return ( <div className="App"> { timeCountdown > 0 ? ( <Tooltip title={toolTipInfo} placement="top" > <span style={{ color: fontColor }} >{info}</span> </Tooltip> ) : ( <Modal open={true} destroyOnClose={true} getContainer={() => document.body} onCancel={() => { window.location.replace('') }} closable={true} maskClosable={true} title={`授權(quán)到期`} footer={[ <Button size="small" onClick={() => { window.location.replace('') }}> 取消 </Button>, ]} width={215} bodyStyle={{ padding: 0 }} > <div> 您的訪客模式到期了 </div> </Modal> ) } </div> ) } } export default App;
2. index.tsx中使用App.tsx
// index.tsx root.render( <App timeRemain={5300} level={false}/> ); // 這里去掉了嚴(yán)格模式
兩點(diǎn)內(nèi)容
1. setInterval只是作為組件內(nèi)部狀態(tài)更新的觸發(fā)器
沒有使用定時(shí)器的計(jì)算值作為渲染的源,因?yàn)?code>setInterval的計(jì)算值是不準(zhǔn)確的,特別是頁面切換到后臺(tái)的時(shí)候;因此使用Date的差值作為計(jì)算依據(jù).
2. antd組件Tooltip和Modal的使用
倒計(jì)時(shí)組件使用antd組件Tooltip和Modal制作,當(dāng)其訪客時(shí)間耗盡之后會(huì)彈出模態(tài)框通知用戶退出. 這種交互模式比較友好.
3. 倒計(jì)時(shí)立即更新
使用this.setState(()=>({}))
而不是this.setState({})
,以確保時(shí)間立即更新.
4. 可控FPS
由于1,可知減少觸發(fā)器setInterval
的間隔,可以使倒計(jì)時(shí)顯示更加絲滑.
到此這篇關(guān)于React實(shí)現(xiàn)倒計(jì)時(shí)功能組件的文章就介紹到這了,更多相關(guān)React倒計(jì)時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
webpack打包react項(xiàng)目的實(shí)現(xiàn)方法
這篇文章主要介紹了webpack打包react項(xiàng)目的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06React Fiber中面試官最關(guān)心的技術(shù)話題
這篇文章主要為大家介紹了React Fiber中面試官最關(guān)心的技術(shù)話題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06手動(dòng)用webpack搭建第一個(gè)ReactApp的示例
本篇文章主要介紹了手動(dòng)用webpack搭第一個(gè) ReactApp的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04react實(shí)現(xiàn)組件狀態(tài)緩存的示例代碼
本文主要介紹了react實(shí)現(xiàn)組件狀態(tài)緩存的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02react+antd4實(shí)現(xiàn)優(yōu)化大批量接口請(qǐng)求
這篇文章主要為大家詳細(xì)介紹了如何使用react hooks + antd4實(shí)現(xiàn)大批量接口請(qǐng)求的前端優(yōu)化,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-02-02React Native中NavigatorIOS組件的簡單使用詳解
這篇文章主要介紹了React Native中NavigatorIOS組件的簡單使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01