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

71
Views
Load More with paginated api

I'm making a Blog Web Page and the API I'm using to build it, it's already paginated so it returns 10 objects in every request I make. But the client wants the page to have a "load more" button, in each time the user click on it, it will keep the already loaded data and load more 10 objects. So far, I've made the button call more 10 new objects, everytime I clicked on it but I also need to keep the already loaded data.

This is my file so far: MainPage.js

import React, { useState, useEffect} from 'react';
const MainPage = () => {
    const [blogs, setBlogs] = useState('');
    const [count, setCount] = useState(1);
    useEffect(() => {
        fetch("https://blog.apiki.com/wp-json/wp/v2/posts?_embed&categories=518&page="+count)
        .then((response) => response.json())
        .then((json) => {
            console.log(json)
            setBlogs(json)
        })
    }, [count])

    const clickHandler = () => {
        console.log(count);
        return setCount( count+1)
    }
    return (
        <div>
            <p>All the recent posts</p>
            { blogs && blogs.map((blog) => {
                return (
                    <div key={blog.id}>
                        <img width="100px" src={blog._embedded["wp:featuredmedia"][0].source_url}/>
                        <p>{blog.title["rendered"]}</p>
                    </div>
                )
            })
            
            }
            <button onClick={clickHandler}>LoadMore</button>
        </div>
    )
}

export default MainPage;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The idea is pretty simple. Just concatenate arrays using the Spread syntax as follows.

var first =[1, 2, 3];
var second = [2, 3, 4, 5];
var third = [...first, ...second];

So, do this thing when you're clicking the load more button.

Here I've come up with handling the whole thing:

Firstly, I will call a function inside the useEffect hook to load some blog posts initially. Secondly I've declared an extra state to show Loading and Load More text on the button.

Here is the full code snippet:

import React, { useState, useEffect } from "react";

const MainPage = () => {
  const [loading, setLoading] = useState(false);
  const [blogs, setBlogs] = useState([]);
  const [count, setCount] = useState(1);

  useEffect(() => {
    const getBlogList = () => {
      setLoading(true);
      fetch(
        "https://blog.apiki.com/wp-json/wp/v2/posts?_embed&categories=518&page=" +
          count
      )
        .then((response) => response.json())
        .then((json) => {
          setBlogs([...blogs, ...json]);
          setLoading(false);
        });
    };

    getBlogList();
  }, [count]);

  return (
    <div>
      <p>All the recent posts</p>
      {blogs &&
        blogs.map((blog) => {
          return (
            <div key={blog.id}>
              <img
                width="100px"
                src={blog._embedded["wp:featuredmedia"][0].source_url}
              />
              <p>{blog.title["rendered"]}</p>
            </div>
          );
        })}
      {
        <button onClick={() => setCount(count + 1)}>
          {loading ? "Loading..." : "Load More"}
        </button>
      }
    </div>
  );
};

export default MainPage;

about 4 years ago · Juan Pablo Isaza Report

0

According to React documentation:

If the new state is computed using the previous state, you can pass a function to setState.

So you could append newly loaded blog posts to the existing ones in useEffect like this:

setBlogs((prevBlogs) => [...prevBlogs, ...json])

I would also set the initial state to an empty array rather than an empty string for consistency:

const [blogs, setBlogs] = useState([]);
about 4 years ago · Juan Pablo Isaza Report
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!