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

283
Visualizações
I send data as props to components. But, only the last component showing data

I wanted to build a food web app. I wanted to fetch data from api and send to different components. When components will be clicked they will show data according to the props send to them.

But, here the problem is only the last component is showing data. others are not.

Image1(Home.js) Home.js Image2(Home.js-remaining) Home.js- remaining Image3(Breakfast component) Breakfast component Image4(Lunch Component) Lunch Component Image5(Dinner component) Dinner Component Image6(Output1) Here breakfast's data is showing in console. But, in UI breakfast data is not showing Image7(Output2) Here Lunch's data is showing in console. But, in UI Lunch data is not showing Image8(Output3) Here Dinner's data is showing perfectly.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Ok so a few things:

  1. Post your actual code, not pictures of code
  2. The .map function if for building a new array, it's not really meant for looping over an array, use something like .forEach for that
  3. Each .map iteration you're calling setBreakfast (or one of the other ones) and overwriting the previous state value. You want to build an array and set the state to that array
  4. As a general rule don't update React state in a loop, it's inefficient and can causes issues. Run a loop to build a value/data structure, then set the state to that

What you want to do it something like this:

.then(
    setBreakfast(data.map(food => food.breakfast));
    setLunch(data.map(food => food.lunch));
    setDinner(data.map(food => food.dinner));
);

That way you're setting the state value to the return value of .map, which is an array containing all the corresponding meal values for that meal type.

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