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

103
Visualizações
How to return the first 2 objects of an array of 4 objects

I want to return the first 2 objects of an array of 4 objects then add the other two objects with 5 seconds in between.

note: I am reversing the copied array revEvents with reverse() as items are in descending order by date/time, the most recent item goes on top.

My current issue is the first two objects are displayed ok, then after 5 seconds, it loads only the third object and it stops.

 useEffect(() => {
   let ms = 3000
   let i = 0
   let displayedEvents = [...props].splice(i, 2)
   setEventsProps(displayedEvents.reverse())
   const interval = setInterval(() => {
     if (++i <= props.length) {
       displayedEvents = props.splice(0, i + 2)
       setEventsProps(displayedEvents.reverse())
      }
    }, ms)
    return () => { clearInterval(interval) }
    }, [])

 //JSX as below
 displayedEvents.map(event () => ...etc

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

0

I'd like to share an improved solution.

   useEffect(() => {
        const ms = 5000
        let i = 2
        const displayedEvents: IEventsProps = props.slice(0, 2)
        setEventsProps(displayedEvents.reverse())
        let interval: NodeJS.Timeout = setInterval((): void => {
            if (i < props.length) {
                displayedEvents.push(props[i])
                setEventsProps(displayedEvents.reverse())
                i += 1
            } else {
                clearInterval(interval)
                interval = null
            }
        }, ms)
        return () => { if (interval) { clearInterval(interval) } }
    }, [])

This avoids making unnecessary new arrays by mutating the same array, and also clears the interval when the work is done, so it doesn't run forever.

about 4 years ago · Juan Pablo Isaza Relatório

0

Fixed this issue, instead of using the splice() function I needed to use the slice() function instead.

The code remains the same, added typescript for anyone who find this.

   useEffect(() => {
        let ms: number = 5000
        let i: number = 0
        let displayedEvents: IEventsProps = [...props].slice(i, 2)
        setEventsProps(displayedEvents.reverse())
            const interval: NodeJS.Timeout = setInterval((): void => {
                if (++i <= props.length) {
                    displayedEvents = props.slice(0, i + 2)
                    setEventsProps(displayedEvents.reverse())
                }
            }, ms)
        return () => { clearInterval(interval) }
    }, [])

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