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

255
Visualizações
How to display an error when rendering a paginated list of items in React?

I have this component:

const PostsList = ( { id } ) => {

   const { pages, size, setSize } = usePages( "/all-posts" )

   return(
      <div id={ id } className={ styles.PostsListContainer }>
         {
            pages?.map( ( { data } ) =>
               data?.posts?.edges?.map( ( { node: post } ) =>  
                   <PostCard key={ post?.id } title={ post?.title } />
               )
            )
         }
         <button onClick={ () => setSize(size+1)}>Load more</button>
      </div>
   )

}

export default PostsList

The usePages hook makes a Graphql request and returns pages, an array of objects, each object representing a page of results, which looks like this:

const pages = [
   {
      data: {
         posts: {
            edges: [
               { node: { id: 1 } },
               { node: { id: 2 } },
               { ... }
            ]
         }
      }, 
      errors: [ ... ] 
   },
   {
      data: { ... },
      errors: { ... },
   }
]

Now, suppose that when fetching page 3 some error occurs, and data.posts is empty: how do I append an error message to the list?

I tried doing this:

const PostsList = ( { id } ) => {

   const { pages, size, setSize } = usePages( "/all-posts" )

   return(
      <div id={ id } className={ styles.PostsListContainer }>
         {
            pages?.map( ( { data } ) => {

               if( data?.posts ) {
                  return data?.posts?.edges?.map( ( { node: post } ) =>  
                     <PostCard key={ post?.id } title={ post?.title } />
                  )
               } else {
                  return <p>Error when fetching the posts</p>
               }
            
            })
         }
         <button onClick={ () => setSize(size+1)}>Load more</button>
      </div>
   )

}

export default PostsList

but sure enough the error message replaces the whole list, meaning even the previous pages which were successfully fetched. I can't either set a piece of state to use as a flag and update it in the render method when data.posts is empty, 'cause that creates the "too many re-renders" error.

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