使用 react 和循环进度组件构建性能仪表板在本博客中,我们将探讨如何使用 react 构建性能指标仪表板。仪表板显示不同绩效指标(例如可访问性、seo 和最佳实践)的循环进度指示器。进度指示器逐渐填满,模拟加载动画。该项目使用 tailwind css 进行样式设计,并组合了多个组件来创建灵活且可重用的界面。项目概况我们将创建两个主要组件:circularprogress – 显示给定百分比的圆形进度条。仪表板 – 显示不同指标的多个进度条,例如性能、可访问性等。circularprogress 组件circularprogress 组件处理圆形进度条,它以指定的百分比进行动画处理。该组件采用以下属性:innercirclecolor:圆形进度内的背景颜色。百分比:完成的百分比。progresscolor: 进度条的颜色。bgcolor: 进度区域外的背景颜色。textcolor: 百分比文本的颜色。title:指标的标题。代码实现import react, { useeffect, useref, usestate } from 'react';interface circularprogressprops { innercirclecolor: string; percentage: number; progresscolor: string; bgcolor: string; textcolor: string; title: string;}const circularprogress: react.fc<circularprogressprops> = ({ innercirclecolor, percentage, progresscolor, bgcolor, textcolor, title,}) => { const [currentpercentage, setcurrentpercentage] = usestate(0); const innercircleref = useref<htmldivelement null>(null); useeffect(() => { const speed = 50; // speed of the animation const increment = () => { setcurrentpercentage((prev) => { if (prev >= percentage) return percentage; return prev + 1; }); }; const interval = setinterval(increment, speed); return () => clearinterval(interval); }, [percentage]); return ( <div classname="flex flex-col justify-center gap-2"> <div classname="relative flex items-center justify-center w-12 h-12 rounded-full" style="{{" background:> <div classname="absolute w-[calc(100%_-_6px)] h-[calc(100%_-_6px)] rounded-full" style="{{" backgroundcolor: innercirclecolor ref="{innercircleref}"></div> <p classname="relative text-[16px] font-semibold" style="{{" color: textcolor> {currentpercentage}% </p> </div> <p classname="text-[10px] font-semibold text-center">{title}</p> </div> );};export default circularprogress;</htmldivelement></circularprogressprops>登录后复制仪表板组件dashboard 组件显示 circularprogress 组件的多个实例,每个实例代表不同的性能指标。代码实现import react from 'react';import circularprogress from './circularprogress';const dashboard: react.fc = () => { return ( <div classname="bg-white flex flex-col items-center border h-auto w-full xl:px-[14rem] lg:px-[5rem] sm:px-0 py-[5rem] justify-center"> <div classname="w-full border rounded"> <div classname="py-12 border-b"> {/* performance metrics */} <div classname="flex flex-wrap justify-center gap-14 items-center"> <circularprogress innercirclecolor="bisque" percentage="{99}" progresscolor="darkorange" bgcolor="bisque" textcolor="darkorange" title="performance"></circularprogress><circularprogress innercirclecolor="bisque" percentage="{96}" progresscolor="darkorange" bgcolor="bisque" textcolor="darkorange" title="accessibility"></circularprogress><circularprogress innercirclecolor="lightgreen" percentage="{90}" progresscolor="limegreen" bgcolor="lightgreen" textcolor="limegreen" title="best practices"></circularprogress><circularprogress innercirclecolor="bisque" percentage="{100}" progresscolor="darkorange" bgcolor="bisque" textcolor="darkorange" title="seo"></circularprogress></div> </div> </div> </div> );};export default dashboard;登录后复制主页组件除了进度条之外,仪表板还包括一个可折叠部分,其中显示有关服务器响应时间的更多详细信息。代码实现 import React, { useState } from 'react';import { IoIosArrowDown, IoIosArrowUp } from 'react-icons/io';const Home: React.FC = () => { const [isExpanded, setIsExpanded] = useState(false); const handleToggle = () => { setIsExpanded(!isExpanded); }; return ( <div classname="rounded-md w-full mb-4" id="server-response-time"> <div classname="flex flex-col border w-full justify-between items-center text-sm text-red-600"> <div classname="flex items-center p-3 justify-between w-full"> <span classname="ml-2 text-gray-800"> <span classname="text-red-700">??</span> Reduce initial server response time <span classname="text-red-500">— Root document took 820 ms</span> </span> <span classname="text-gray-800 cursor-pointer" onclick="{handleToggle}"> {isExpanded ? <ioiosarrowup></ioiosarrowup> : <ioiosarrowdown></ioiosarrowdown>} </span> </div> {isExpanded && ( <div classname="bg-white border-t border-t-blue-600"> <div classname="py-8 pl-12 pr-4"> <p classname="text-[13px] text-gray-700"> Learn more about server response time and performance optimizations.{' '} <a classname="text-blue-500 underline" href="#" target="_blank" rel="noopener noreferrer"> Read more. </a> </p> </div> </div> )} </div> </div> );};export default Home;登录后复制结论此性能仪表板展示了如何在 react 中创建可重用的动画循环进度组件。通过以这种方式构建仪表板,您可以轻松扩展它以跟踪其他性能指标或将其集成到更广泛的应用程序中,使其成为可视化关键指标的强大工具。请随意根据您的项目调整此代码,并享受使用 react 创建性能仪表板! 以上就是如何在Reactjs中制作动态进度条的详细内容,更多请关注我的其它相关文章!
标签:gt,const,进度条,Reactjs,react,组件,动态,仪表板 From: https://www.cnblogs.com/aow054/p/18424957