Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
How can i re-render using useState from inside a callback and a loop on the component

i am trying to do the game of life with react, but is just rendering once instead of several times that was supposed to. Inside handleGame has a setGrid that is what is just working once.

  const handleRules = (neighbors, grid, i, j) => {
    const next = [...grid];
    switch (true) {
      case neighbors > 3:
        next[i][j] = 0;
        break;
      case neighbors < 2:
        next[i][j] = 0;
        break;
      default:
        next[i][j] = 1;
    }
    return next;
  };

  const handleGame = () => {
    for (let row = 0; row < initialRows; row++) {
      for (let col = 0; col < initialCols; col++) {
        //  let current = gridCopy[row][col];
        let next;
        if (
          row === 0 ||
          col === 0 ||
          row === initialRows - 1 ||
          col === initialCols - 1
        ) {
          /* setGrid((current) => {
            next[row][col] = grid[row][col];
            return [...next];
          }); */
        } else {
          setGrid((current) => {
            const neighbors = handleCountNeighbors(current, row, col);
            next = handleRules(neighbors, current, row, col);
            return [...next];
          });
        }
      }
    }
  };

  const handleStart = () => {
    for (let i = 0; i <= gen; i++) {
      handleGame();
    }
  };

Can someone help me? https://codesandbox.io/s/tender-snow-gcknd?file=/src/App.js

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda