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

69
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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