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

96
Visualizações
prop passed to child component not updated after async call

I am new to React and building a movie db app. I have defined my async fetch movies api call in API.js and call this in the Home.js components' useEffect and set the result in the Home.js "trending" state and further pass this into a Grids component. Issue is Grids component always returns empty object for the trending prop passed, even when the trending state is updated. I'm not sure whats happening here. please advise

Home.js

const Home = () => {
  const { searchTerm } = useContext(SearchTermContext);
  const [trending, setTrending] = useState([]);
  const [loading, setLoading] = useState(false);

  useEffect(() => {
    setLoading(true);
    const trendingList = async () => {
      setTrending(await fetchTrending());
      setLoading(false);
    };
    trendingList();
  }, []);

  return (
    <div className="homepage">
      <Hero />
      <p>{trending.length > 0 && trending.length},grids</p>//prints length
      correctly once state updates
      {trending.length > 0 && !loading && <Grids trending={trending} />}
    </div>
  );
};

Grids.js

const Grids = ({ heading = "What's Popular" }, trending) => {
  console.log(trending.length, "in grid"); //prints {},"in grid", even after trending state is updated
  // const thumbElements = trending?.map((item) => {
  //     return <Thumb key={item.id} item={item} />
  // });
  return (
    <div className="grid-section">
      <h3>{heading}</h3>
      <div className="thumbs">{/* {thumbElements} */}</div>
    </div>
  );
};

fetchTrending in API.js

export const fetchTrending = async (mediaType) => {
  if (sessionStorage.getItem("trending")) {
    return await JSON.parse(sessionStorage.getItem("trending"));
  } else {
    try {
      const response = await axios.get(
        `${BASE_URL}/trending/movie/day?api_key=${API_KEY}`
      );
      sessionStorage.setItem("trending", JSON.stringify(response.data.results));
      return response.data.results;
    } catch (error) {
      console.log(error);
    }
  }
};
about 4 years ago · Juan Pablo Isaza
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