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

142
Visualizações
Flatlist item duplication issue

When I render my flatlist, it seems to duplicate items inside it (feedCache has one index and it will render the data for this index twice). Here's the code for the flatlist:

const FeedBody = React.memo(() => {
    return (
        <FlatList
            data={feedCache}
            renderItem={({ item }) => {
                return (
                    <FeedPost
                        item={item}
                    />
                )
            }}
            keyExtractor={item => item.id}
        />
    )
})
return (
    <>
        {feedLoaded && feedCache && feedReturnCache
            ? <FeedBody />
            : <AppLoading />
        }
    </>
)

At the top of the document, in the useEffect, I fetch data from an API and write parts to feedCache and feedReturnCache. Once this completes feedLoaded is set to true and the feed body is supposedly rendered. Despite this, I'm convinced the FeedBody component is still re-rendering due to state updates. This causes duplication of items inside the flatlist. Is there a way to fix/prevent this?

Any help would be appreciated, thanks.

(useEffect code)

const [feedLoaded, setFeedLoaded] = useState(false)
const [feedCache, setFeedCache] = useState(null)
const [feedReturnCache, setFeedReturnCache] = useState(null)

useEffect(() => {
    feedMounted = true
    server("POST", "/content/feed", {
        token: user.token,
        cachePresent: false,
        metric: "new"
    }, (data, http, error) => {
        if (!error) {
            const toBeStatefulFeedReturnCache = new Array()
            for (let i = 0; i < data.length; i++) {
                toBeStatefulFeedReturnCache.push({
                    id: data[i].id,
                    read: false
                })

                if (i + 1 === data.length) {
                    if (feedMounted) {
                        setFeedCache(data) 
                    setFeedReturnCache(toBeStatefulFeedReturnCache)
                        setFeedLoaded(true)
                    }
                }
            }
        } else {
            throw error
        }
    })
    return () => {
        feedMounted = false
    }
}, [])
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try to fix duplication in array with set

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