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

234
Visualizações
How to wait until function returns a value (React js)

Hay everyone, I made a short react js function that called "ChartPage" that gets a value from getCoin function and then passes the value to Chart component I made. But my code for some reason isn't waiting for getCoin to return the value and continues in the code and calls to Chart with an 'undefined' as the value.
Can someone help me make "ChartPage" waits until getCoin functions returns the value that I need to pass?
this is my code:

import React, { Component } from "react";
import "../../App.css";
import Chart from "../Chart";
import { Button } from "../Button.js";
import "./ChartPage.css";
import axios from "axios";

function getCoin(data) {
  var path = `/show-stocks/${data}`;
  axios.get(path).then((response) => {
    return response.data;
  });
}

export default function ChartPage({ data }) {
  var values = getCoin(data); // "values" is the variable I want to pass to "Chart"
  return (
    <>
      <div className="chart-page-container">
        <h1>{data} Price Market Value</h1>
        <div className="chart-container">
          <Chart data={values} /> {/* here I am passing "values" to "Chart" component*/}
        </div>
        <div className="chart-page-btns">
          <Button
            link="/show-stocks/bitcoin"
            className="btns"
            buttonStyle="btn--outline2"
            buttonSize="btn--large2"
          >
            Bitcoin
          </Button>
          <Button
            link="/show-stocks/ethereum"
            className="btns"
            buttonStyle="btn--outline2"
            buttonSize="btn--large2"
          >
            Ethereum
          </Button>
          <Button
            link="/show-stocks/cardano"
            className="btns"
            buttonStyle="btn--outline2"
            buttonSize="btn--large2"
          >
            Cardano
          </Button>
        </div>
      </div>
    </>
  );
}
about 4 years ago · Santiago Gelvez
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