Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
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 Respuestas
Responde la pregunta

0

Try to fix duplication in array with set

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda