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

176
Vistas
React - How to properly load data with pagination

On start, I'm loading the data in useEffect like this:

 const [page, setPage] = useState(1);
 const [perPage, setPerPage] = useState(12);

 useEffect(() => {
    const loadVehicles = async (dealershipId, id) => {
      try {
        const data = await getUserRequests(dealershipId, id, {
          offset: (page - 1) * perPage,
          limit: perPage,
        });

        setRequests(data.results);
      } catch (err) {}
    };

    const loadUser = async (userId) => {
      try {
        const user = await getUserById(userId);

        setSelectedUser(user);
      } catch (err) {}
    };

    const loadData = async () => {
      setIsLoading(true);

      await loadVehicles(dealershipId, userId);
      await loadUser(userId);

      setIsLoading(false);
    };

    loadData();
  }, [dealershipId, page, perPage, userId]);

Here, in the dependecy array I have "page" and "perPage", which are for pagination for vehicles. When I go to, for example, second page:

const changePageHandler = (pageNumber) => {
    setPage(pageNumber);
  };

page is updating, and then useEffect is calling. But the problem is that I just want to call loadVehicles() and not the loadUser() function.

And when I put loadVehicles(dealershipId, userId) function outside of the useEffect, and then call it after setPage(pageNumber), it's not getting the right page, because page is not updated yet.

How can I call only loadVehicles() with the correct page?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

You should use another "useEffect" hook for handling page changes. Also it is better to define your functions outside your hooks so you can use them in other places as well.

const [page, setPage] = useState(1);
const [perPage, setPerPage] = useState(12);
const loadVehicles = async (dealershipId, id) => {
      try {
        const data = await getUserRequests(dealershipId, id, {
          offset: (page - 1) * perPage,
          limit: perPage,
        });

        setRequests(data.results);
      } catch (err) {}
};
const loadUser = async (userId) => {
      try {
        const user = await getUserById(userId);

        setSelectedUser(user);
      } catch (err) {}
 };
const loadData = async () => {
      setIsLoading(true);
      await loadVehicles(dealershipId, userId);
      await loadUser(userId);
      setIsLoading(false);
};
 useEffect(async() => {
    await loadData();
 }, [dealershipId, userId]);
 useEffect(async() => {
    setIsLoading(true);
    await loadVehicles(dealershipId, userId);
    setIsLoading(false);
 }, [page, perPage]);

I am pretty sure you will understand the concept. The code above is just a hint.

about 4 years ago · Santiago Gelvez 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