Component Cell has the state alive which can be true or false.
If alive is true then Cell is rendered as a div with a class alive (think of Conway's Game of Life). The state alive updates every second:
function Cell(props) {
const [alive, setAlive] = useState(
Math.random() < props.aliveProbability / 100
);
useEffect(() => {
const interval = setInterval(
() => setAlive(Math.random() < props.aliveProbability / 100),
1000
);
return () => clearInterval(interval);
}, []);
return (
<div className={"cell " + (alive ? "alive" : "")}>{props.children}</div>
);
}
With a single cell this works fine. But when adding multiple cell components to a grid, the rendering slows down and is happening in sequentially instead of simultaneously. The more cells, the slower it becomes. How can this be resolved?