我需要在输入useEffect钩子时获得帮助,以便使Document.queryselector在代码底部工作.该脚本主要做的是获取数字和一个圆圈进度条,并将数字和圆圈进度条对齐,以同 timeshift 动它递增的每个数字.我只需要帮助添加useEffect挂钩,并使其工作时,页面刷新.:

import { SearchIcon } from "@heroicons/react/outline";
import { useState } from "react";
import News from "./News";
import { AnimatePresence, motion } from "framer-motion"; 
import dynamic from 'next/dynamic'
import {useEffect} from "react";


export default function Widgets({newsResults}) {
  const[articleNum, setArticleNum] = useState(3);
  return (
    <div className="xl:w-[600px] lg:inline ml-8 space-y-5">
        <div className="w-[90%] xl:w-[75%] sticky top-0 bg-white py-1.5 z-50">
            <div className="flex items-center p-3 rounded-full bg-red-300 relative">
                <SearchIcon className="h-5 z-50 text-gray-500 "/>
                <input className="absolute inset-0 rounded-full pl-11 border-gray-500 text-gray-700 focus:shadow-lg focus:bg-white bg-gray-100" type="text" placeholder="Search RUbars" />
            </div>
        </div>

        {/* <div className="text-gray-700 space-y-3 bg-gray-100 rounded-xl pt-2 w-[90%] xl:w-[75%]">
            <h4 className="font-bold text-xl px-4">Whats happening</h4>
        <AnimatePresence>
        {newsResults.slice(0,articleNum).map((article)=>(
          <motion.div key={article.title} initial={{opacity: 0 }} animate={{opacity: 1}} exit={{opacity: 0}} transition={{duration: 1}}>
           <News key={article.title} article={article}/>
           </motion.div>
        ))}
        </AnimatePresence>
        <button onClick={()=>setArticleNum(articleNum + 3)} className="text-rose-300 pl-4 pb-3 hover:text-rose-400">Show more</button>
        </div> */}
        <div className="text-gray-700 space-y-3 bg-gray-100 pt-2 rounded-xl w-[90%] xl:w-[75%]">
          <h4 className="font-bold text-xl px-4">Capacity</h4>
          <div class="container">
            <div class="circular-progress">
            <span class="progress-value">0%</span>
            </div>
            <span className="text">Olde Queens</span>
              <div class="circular-progress">
              <span className="progress-value">0%</span>
            </div>
            <span className="text1">Golden Rail</span>
            <div class="circular-progress1">
              <span className="progress-value1">0%</span>
            </div>
            <span className="text">Huey&apos;s Knight Club</span>
            <button className="text-rose-300 pl-4 pb-3 hover:text-rose-400">Show more</button>
          </div>
        </div>
    </div>
  )
}

let circularProgress = document.querySelector(".circular-progress"),
    progressValue = document.querySelector(".progress-value");

let progressStartValue = 0,
    progressEndValue = 24,
    speed = 40;

let progress = setInterval(() =>{
    progressStartValue++;

    progressValue.textContent = `${progressStartValue}%`
    circularProgress.style.background = `conic-gradient(#E0115F ${progressStartValue * 3.6}deg, #ededed 0deg)`

    if(progressStartValue == progressEndValue){
        clearInterval(progress);
    }
    console.log(progressStartValue);

}, speed);

我知道它必须看起来像这样:

  useEffect(() => {
    let circularProgress = document.querySelector(".circular-progress");
    console.log(circularProgress)
  }, []);

但我真的不确定如何将其插入底部.

推荐答案

将状态用于进度,并将useEffect函数放在小部件组件中,当您的状态更改时,重新呈现您的组件以更新视图模板.

Code Edit For Multiple Progress with different end value and speed

export default function Widgets({newsResults}) {
  const[articleNum, setArticleNum] = useState(3);
  const[progressValue1, setProgressValue1] = useState(0);
  const[progressValue2, setProgressValue2] = useState(0);
  const[progressValue3, setProgressValue3] = useState(0);

  let progressEndValue1 = 24, progressEndValue2 = 34, progressEndValue3 = 44,
  progressStartValue1 = 0, progressStartValue2 = 0, progressStartValue3 = 0;

useEffect(() => {
  const progress1 = setInterval(() =>{
    if(progressEndValue1 > progressStartValue1){
      progressStartValue1++;
      setProgressValue1(progressStartValue1);
      console.log(progressValue1)
    }
    else{
      clearInterval(progress1);
    }
  }, 200);
  const progress2 = setInterval(() =>{
    if(progressEndValue2 > progressStartValue2){
      progressStartValue2++
      setProgressValue2(progressStartValue2);
    }
    else{
      clearInterval(progress2);
    }
  }, 300);
  const progress3 = setInterval(() =>{
    if(progressEndValue3 > progressStartValue3){
      progressStartValue3++;
      setProgressValue3(progressStartValue3);
    }
    else{
      clearInterval(progress3);
    }
  }, 400);
}, []);
  return (
    <div className="xl:w-[600px] lg:inline ml-8 space-y-5">
        <div className="w-[90%] xl:w-[75%] sticky top-0 bg-white py-1.5 z-50">
            <div className="flex items-center p-3 rounded-full bg-red-300 relative">
              <SearchIcon className="h-5 z-50 text-gray-500 "/>
                <input className="absolute inset-0 rounded-full pl-11 border-gray-500 text-gray-700 focus:shadow-lg focus:bg-white bg-gray-100" type="text" placeholder="Search RUbars" />
            </div>
        </div>

        {/* <div className="text-gray-700 space-y-3 bg-gray-100 rounded-xl pt-2 w-[90%] xl:w-[75%]">
            <h4 className="font-bold text-xl px-4">Whats happening</h4>
        <AnimatePresence>
        {newsResults.slice(0,articleNum).map((article)=>(
          <motion.div key={article.title} initial={{opacity: 0 }} animate={{opacity: 1}} exit={{opacity: 0}} transition={{duration: 1}}>
           <News key={article.title} article={article}/>
           </motion.div>
        ))}
        </AnimatePresence>
        <button onClick={()=>setArticleNum(articleNum + 3)} className="text-rose-300 pl-4 pb-3 hover:text-rose-400">Show more</button>
        </div> */}
        <div className="text-gray-700 space-y-3 bg-gray-100 pt-2 rounded-xl w-[90%] xl:w-[75%]">
          <h4 className="font-bold text-xl px-4">Capacity</h4>
          <div className="container">
            <div className="circular-progress" style={{background: `conic-gradient(#E0115F ${progressValue1 * 3.6}deg, #ededed 0deg)`}}>
            <span className="progress-value">{progressValue1}%</span>
            </div>
            <span className="text">Olde Queens</span>
              <div className="circular-progress" style={{background: `conic-gradient(#E0115F ${progressValue2 * 3.6}deg, #ededed 0deg)`}}>
              <span className="progress-value">{progressValue2}%</span>
            </div>
            <span className="text1">Golden Rail</span>
            <div className="circular-progress1" style={{background: `conic-gradient(#E0115F ${progressValue3 * 3.6}deg, #ededed 0deg)`}}>
              <span className="progress-value1">{progressValue3}%</span>
            </div>
            <span className="text">Huey&apos;s Knight Club</span>
            <button className="text-rose-300 pl-4 pb-3 hover:text-rose-400">Show more</button>
          </div>
        </div>
    </div>
  )
}

Reactjs相关问答推荐

Reaction Native:在初始获取后,Reducer变为未定义

为多租户SSO登录配置Azure AD应用程序

在REACT-RUTER-DOMV6中使用通用页面包装组件有问题吗?

GoLang有条件地为.js.gz提供服务(如果它存在),否则为.js

Reaction上下文值无法传递给其他组件

无法找出状态正在被更改的位置

状态更改时的rtk查询触发器

悬停轴时重新绘制自定义参照线

在react的useEffect钩子中,我的函数被调用,但只有第一个函数能够改变状态,第二个函数无法改变状态.

如何更新redux状态存在的输入框

无法使axiosmockadapter正常工作

如何根据用户在react.js中的 Select , Select 多个心形图标?

如何在next.js 13中仅在主页导航栏上隐藏组件?

如何修改 react/jsx-no-useless-fragment 规则以允许 <>{children}

如何使用 redux 和 react-plotly.js

在准备回调中使用状态属性(Redux 工具包)

从其他组件切换 FieldSet

React Native PermissionsAndroid 不包括 READ_MEDIA_IMAGES.如何解决这个问题?

如何从一组对象映射 MUI 5 图标并更改其 colored颜色 ?

gtag:为什么谷歌分析即使没有被授权也会收集数据