React實現(xiàn)倒計時功能組件
故事背景
倒計時(或者時間顯示)是日常開發(fā)中的常見任務
常用在權限控制或者日期顯示上
由于經(jīng)常會使用到,所以將其分裝成一個組件,方便以后復用
項目結(jié)構搭建
npx create-react-app test-countdown --template=typescript yarn add antd
倒計時組件
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)時間
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}小時 ${minutes}分鐘 ${remainingSeconds}秒`;
return formattedTime;
}
// 倒計時組件props格式
type IProps = {
level: boolean;
timeRemain: number;
}
// 倒計時組件state格式
type IState = {
level: boolean;
timeRemain: number;
lastMountedTime: number;
timeCountdown: number;
timer: any;
}
// 倒計時組件
class App extends React.PureComponent<IProps, IState> {
state = {
lastMountedTime: 0,
timeRemain: 0,
timeCountdown: 9999,
level: false,
timer: null,
}
// 多余的構造函數(shù)
constructor(props: IProps) {
super(props);
}
// 組件構建完畢之后需要將props中設置的等級,倒計時存到state中去
// 此外還需要將當?shù)貢r間存到state中去作為之后計算的參考
// 最后開啟定時器,并將定時器的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,
})
}
// 在組件卸載之前需要釋放定時器
componentWillUnmount(): void {
if(this.state.timer) clearInterval(this.state.timer);
}
// 如果用戶是VIP則tooltip上不顯示訪問剩余時間
// 如果用戶是訪客,則字體變成紅色,tooltip顯示倒計時
// 如果倒計時為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={`授權到期`}
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}/>
);
// 這里去掉了嚴格模式兩點內(nèi)容
1. setInterval只是作為組件內(nèi)部狀態(tài)更新的觸發(fā)器
沒有使用定時器的計算值作為渲染的源,因為setInterval的計算值是不準確的,特別是頁面切換到后臺的時候;因此使用Date的差值作為計算依據(jù).
2. antd組件Tooltip和Modal的使用
倒計時組件使用antd組件Tooltip和Modal制作,當其訪客時間耗盡之后會彈出模態(tài)框通知用戶退出. 這種交互模式比較友好.
3. 倒計時立即更新
使用this.setState(()=>({}))而不是this.setState({}),以確保時間立即更新.
4. 可控FPS
由于1,可知減少觸發(fā)器setInterval的間隔,可以使倒計時顯示更加絲滑.
到此這篇關于React實現(xiàn)倒計時功能組件的文章就介紹到這了,更多相關React倒計時內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
react+antd4實現(xiàn)優(yōu)化大批量接口請求
這篇文章主要為大家詳細介紹了如何使用react hooks + antd4實現(xiàn)大批量接口請求的前端優(yōu)化,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2024-02-02
React Native中NavigatorIOS組件的簡單使用詳解
這篇文章主要介紹了React Native中NavigatorIOS組件的簡單使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01

