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

207
Visualizações
React tsx Render Intervally changing list

I want to make a Game where some items are falling from the top and if they are clicked they disappear and raise the score, but I cannot see the items.

I use the setInterval to push the random items into the list at intervals and render the items list.

If I put a fixed list, it shows the items but all at the same time. I want the items to keep adding in continuously. It's okay to not use the interval. I just don't know the other way.

codes:

var DgArray:string[] = []

const [sec, setSec] = useState();

const time: any = useRef(10);
const timerId: any = useRef('startTimer');

useEffect(() => {
    timerId.current = setInterval(() => {
        setSec(time.current);
        time.current -= 1;
        console.log(time.current)
        DgArray.push(RandomKind()) //RandomKind returns a string
        console.log(DgArray)
    }, 1000);

    return () => clearInterval(timerId.current);
}, []);


if (time.current <= 0) {
        console.log("TimeOut");
        clearInterval(timerId.current);
    }
}, [sec]);

the return

<IonPage className="Game">
        <h1 id="startTimer">{Number((sec / 10).toFixed(0))}</h1>
        { DgArray.map((value, idx) => <Dg kind={value} DgNum={idx}></Dg>)}
</IonPage >

in console, the DgArray is keep adding.

and I found using the breakpoint in Dg component that it goes to the Dg component, but it does not return even in the component.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your problem is likely that your IonPage (which I will assume is designed to render children) does not know to re-render when DgArray changes because it is outside of the state of any component.

One approach to fix this would be to have DgArray be the state of a new component (possibly wrapping your IonPage so you can use the same DgArray.map) and you pass in setDgArray to your component with setInterval to update it there.

about 4 years ago · Juan Pablo Isaza Relatório
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