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

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

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 Denunciar

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 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