React倒计时模板组件(权限管理)

React倒计时模板组件(权限管理)

故事背景

  • 倒计时(或者时间显示)是日常开发中的常见任务
  • 常用在权限控制或者日期显示上
  • 由于经常会使用到,所以将其分装成一个组件,方便以后复用

项目结构搭建

shell 复制代码
npx create-react-app test-countdown --template=typescript
yarn add antd

倒计时组件

1. App.tsx中的内容

typescript 复制代码
// App.tsx
/* eslint-disable @typescript-eslint/no-useless-constructor */
import React from 'react';
import { Tooltip, Modal, Button } from "antd";
import './App.css';

// 秒转时间
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,
  }

  // 多余的构造函数
  constructor(props: IProps) {
    super(props);
  }

  // 组件构建完毕之后需要将props中设置的等级,倒计时存到state中去
  // 此外还需要将当地时间存到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则弹出模态框通知用户试用结束
  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

typescript 复制代码
// index.tsx
root.render(
    <App timeRemain={5300} level={false}/>
);

// 这里去掉了严格模式

两点内容

1. setInterval只是作为组件内部状态更新的触发器

没有使用定时器的计算值作为渲染的源,因为setInterval的计算值是不准确的,特别是页面切换到后台的时候;因此使用Date的差值作为计算依据.

2. antd组件Tooltip和Modal的使用

倒计时组件使用antd组件Tooltip和Modal制作,当其访客时间耗尽之后会弹出模态框通知用户退出. 这种交互模式比较友好.

3. 倒计时立即更新

使用this.setState(()=>({}))而不是this.setState({}),以确保时间立即更新.

4. 可控FPS

由于1,可知减少触发器setInterval的间隔,可以使倒计时显示更加丝滑.

相关推荐
恋猫de小郭5 小时前
Flutter Zero 是什么?它的出现有什么意义?为什么你需要了解下?
android·前端·flutter
崔庆才丨静觅12 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby606112 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了13 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅13 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅13 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅13 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment13 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅14 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊14 小时前
jwt介绍
前端