Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!