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

74
Vistas
Have multiple GET requests and call state setter once they have all completed

I'm trying to render a list of favorite movies based on signed-in user's list.The flow is following:

  1. I have ID of favorited movies in the firebase DTB for each user
  2. User visits the "favorited" page and the collection of favorited movies is updated
  3. Then the API call for movieDB is called for each movie to render movie list

Unfortunately I was able to update the array of objects only via push, which resulted in calling setContent(items) for every item which means that content variable exists even with 1st iteration, leaving the option to render the "trending" div as value is truth not rendering full content.

How can I either refactor the useEffect? Change the render conditions for "Trending" to be sure, that all values in content var are finished updating via API?

const Favorite = () => {
  const { user } = useAuthContext();
  const { documents: movies } = useCollection("favMovies", ["uid", "==", user.uid]); // only fetch movies for loged user

  const [page, setPage] = useState(1);
  const [content, setContent] = useState([]);
  const [numOfPages, setNumOfPages] = useState();

  useEffect(() => {
    const items = [];

    movies &&
      movies.forEach(async (movie) => {
        try {
          const response = await axios.get(
            `https://api.themoviedb.org/3/${movie.mediaType}/${movie.id}?api_key=${process.env.REACT_APP_API_KEY}&language=en-US`
          );
          items.push(response.data);
          setContent(items);
        } catch (error) {
          console.error(error);
        }
      });
  }, [movies]);

  return (
    <div>
      <span className="pageTitle">Trending</span>

      <div className="trending">
        {content &&
          content.map((con) => (
            <SingleContent
              key={con.id}
              id={con.id}
              poster={con.poster_path}
              title={con.title || con.name}
              date={con.first_air_date || con.release_date}
              mediaType={con.media_type}
              voteAverage={con.vote_average}
            />
          ))}
      </div>

      <CustomPagination setPage={setPage} />
    </div>
  );
};

export default Favorite;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can overcome your problem by using Promise.all(). For that change your useEffect code to:

useEffect(() => {
  const fectchMovies = async () => {
    if (!movies) return;
    try {
      const promises = movies.map((movie) =>
        axios.get(
          `https://api.themoviedb.org/3/${movie.mediaType}/${movie.id}?api_key=${process.env.REACT_APP_API_KEY}&language=en-US`
        )
      );
      const content = await Promise.all(promises);
      setContent(content.map(c => c.data));
    } catch (error) {
      console.error(error);
    }
  };
  fectchMovies();
}, [movies]);
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