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

155
Vistas
One fetch request creates many GET requests on server

Here is my code:

import React from "react";
import { useState, useEffect } from "react";
import TutorialList from "./TutorialList";
import PropTypes from "prop-types";

const Home = () => {
  const [tutorials, setTutorials] = useState(null);

  useEffect(() => {
    console.log("Fetching data");
    fetch("http://192.168.212.52:8080/api/tutorials/all/")
      .then((res) => {
        return res.json();
        console.log("Getting json from response");
      })
      .then((data) => {
        console.log(data);
        setTutorials(data);
      });
  }, []);

  return (
    <div className="home">
      {console.log(tutorials)}
      {tutorials && (
        <TutorialList tutorials={tutorials} title={"All tutorials"} />
      )}
    </div>
  );
};
Home.propTypes = {
  title: PropTypes.string,
};
export default Home;

I expect this to make 1 get request to the server, which returns the data that is then rendered with react.

What it actually does is make more than 10 requests to the server, still rendering the data after all the requests finish. I can see the requests both from the server logs and from the browser networking dev tools.

However, the Fetching data and Getting json from response logs only execute once, as expected.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Since fetch requests a resource from the network, it runs asynchronously. This said, if you want to get to run it inside a useEffect it might be that if you wrap it in an async function it should work. However, keep in mind that it's not the argument of this hook itself async but another function that you define inside. For instance,

useEffect(() => {
    console.log("Fetching data");
    async function retrieveData() {
      const json = await fetch("http://192.168.212.52:8080/api/tutorials/all/")
      const data = await response.json()
      setTutorials(data)
      console.log("Got json from response");
    }
    retrieveData()
  }, []);

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