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

146
Visualizações
React firebase doesn't await

I want to get data from firebase and map it and render a table with it but the firebase function returns data after the page loads.

    const [datas, setData] = useState();
    var ref = db.ref("something");

  
    useEffect(() => {
      const fetchData = async () => {
        await ref.once("value").then((snapshot) => {
          const fetched = snapshot.val();
          var feed = {'name':'shoe','remark':'remarka','price':'pricea','photo':'photo','amount':'350'};
          console.log('fetched', fetched)
          setData(feed);
        }); 
      };
      fetchData();
    }, []);

now if I make a var here let's say var text= datas and console.log it it will return undefined the feed var is just some dummy text

{dummy.map((data, key) => {
//render table
})

is there any way to make the page wait for the function to finish the useState ?

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

0

Use the AND operator (&&) which only executes the right operand when the left operand is truthy. It is not necessary to make a variable called dummy.

{
  datas && datas.map(data=>{
     console.log(data);
  });
}
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