I was creating a progress bar component with Next.js and tailwind.css and using JavaScript setInterval()
function to animate it. Here is the code below:
import React, { useState } from "react";
const ProgressBar = () => {
// starting at 1% of the width of the progress bar
let [progress, setProgress] = useState(1);
function handleClick() {
let i = setInterval(() => {
setProgress(progress++);
},100)
if (progress == 100) {
clearInterval(i);
}
}
return (
<>
<div className={`bg-gray-300 w-full`}>
<div className={ `h-[30px] w-[${progress}%] bg-green-500`}></div>
</div>
<button onClick={handleClick}>Click me</button>
</>
);
};
export default ProgressBar;
Now the problem is that when I run the development server to test it. It appears as if the progress is completed even before I click the button. It shows the whole width of the progress bar green instead of 1% of its width before clicking the button and sometimes I get some reference errors when I try to use methods such as global variables instead of hooks and even DOM methods such as document.getElementsByClassName().style.width;
and so on please help me and you can copy it to your workspace and run the development server to see the result. Thank you.
I tried manipulating the DOM using methods like document.getElementById()
or document.getElementsByClassName()
and global variables and function-scoped variables and react hooks like useState()
but it gives me reference error to tell me that the variable of the hook that holds the initial value is not defined or global variables and function-scoped variables that I declared at the top of my component to manipulate later are not defined or it shows the progress bar completed even before clicking the button as if the setInterval
has no effect
2
Answers
The setInterval function captures the initial value of progress due to closures, which means that the interval function continues to use the initial value throughout its lifetime.
Firstly, try and avoid direct access to the DOM, it’s very rare you need to do this, and if you do you should use
refs
to do it.There are a few issues with your progress bar,
setProgress(progress++);
<– progress here is scoped to the function, so your constantly sayingsetProgress(1++)
, IOW: it can never get past 2. The solution here to avoid scope is just use the callback versionsetProgress(progress => progress + 1);
React component’s are designed to be unmounted, your current implementation doesn’t take that into account, IOW: if you start an interval it will continue to 100 even when the component is not visible. The solution to this is to use
useEffect
andReact.useRef
, theuseEffect
hook can be used to do something before the component is unmounted. The ref is used to keep track of the interval ID. (note: refs can store more than just DOM references).Below is an example, I’v not used tailwind here, but you should be able to replace with it pretty easy. In the example I’ve also used a checkbox to mount / unmount for testing, if you un-comment what’s in the
useEffect
you will see in your browsers console that thesetInterval
will continue to run even when um-mounted (before 100%), not good.ps. Just click the progress bar to make it start, there is also a check to make if so you can start it again until it’s complete.