I have a dice component, and i want it to spit random values n times, and after that show a static value, and this should happen each time props is updated. so i used setInterval in the following code:
//iter - keep count on how many times we rendered a value
const [iter, setIter] = useState(0);
//keep the intervalId given throughout the renderes
const [intervalId, setIntervalId] = useState(undefined);
//check each render(iter update) if iter has reached 0, to clear the interval
useEffect(() => {
if (iter <= 0) {
clearInterval(intervalId);
}
}, [iter]);
//if the props updated, call roll dice
useEffect(() => {
rollDice();
}, [props]);
const rollDice = () => {
const interval = setInterval(() => {
//reduce iter every 100ms
setIter((prev) => prev - 1);
}, 100);
//run this interval 10 times
setIter(10);
setIntervalId(interval);
};
This is what the component returns:
{props.values.map((val, i) => (
<FontAwesomeIcon
key={i}
//randomize icons
icon={dice[iter ? Math.floor(Math.random() * 5) : val - 1]}
size={props.size || "4x"}
color={props.color || "white"}
/>
))}
But for some reason i get an infinite loop, the first useEffect keeps firing. Why does this happen and how can i avoid this kind of a bug in the future?
Thank you.