I have 3 components with different numbers. What I want is something like this:
- All 3 numbers should increase for 2 seconds.
- I want this animation effect to end at the same time.
- I don't want to use another library.
What should I consider? here is my trial. I would appreciate it if you could give me some points to consider.
https://jsfiddle.net/pizza_hamburger_chicken/ptav9wyg/7/
const {useState, useEffect} = React;
const AnimationNumber = ({ number }) => {
// ------------ useRef
// ------------ useState
const [numberForCount, setNumberForCount] = useState(0);
// ------------ useEffect
useEffect(() => {
let count = 0;
const id = setInterval(() => {
if (count >= number) {
clearInterval(id);
} else {
console.log(++count);
setNumberForCount(count);
}
}, 0.01);
}, []);
// ------------ methods
// ------------ event methods
// ------------ api methods
return (
<div
data-target={number}
>
<strong>
<span>{numberForCount}</span>
</strong>
</div>
);
};
function Example() {
return (
<div>
<AnimationNumber number={700} />
<AnimationNumber number={100} />
<AnimationNumber number={470} />
</div>
);
}
ReactDOM.render( <Example />, document.getElementById('root') );