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

191
Visualizações
How to show temporary message in ReactJS until API endpoint comes back

In my helperfile.js file, I have an react bootstrap and overlay trigger as follows. The flaskResult variable is a prop that is passed in.

  const popover = (
    <Popover id="popover-basic">
      <Popover.Header as="h3">Num Nodes</Popover.Header>
      <Popover.Body>
        The number of nodes is: {flaskResult}
      </Popover.Body>
    </Popover>
  );
...
          <OverlayTrigger rootClose trigger="click" placement="left" overlay={popover}>
            <Button variant="outline-primary"  onClick = { (e) => {onCallEndpoint({endpoint:"get_num_nodes"})}} >Count Num Nodes</Button>
          </OverlayTrigger>

Notice that the Button calls an onCallEndpoint. That endpoint hits a flask API to do some computations. Some computations are very quick and the result is almost instant. Others are very slow and the result takes several seconds.

How would I show "processing" or a spinning circle while flask executes?

Here's the api call in app.js

  const onCallEndpoint = async (props) => {
    const {endpoint} = props;
    try {
        const flow = reactFlowInstance.toObject();
        const token = await getAccessTokenSilently();
        const response = await fetch(
          `${serverUrl}/${props.endpoint}`,
          {
            headers: {
             'Content-Type': 'application/json',
              Authorization: `Bearer ${token}`,
            },
            method: 'POST',
            body:JSON.stringify(flow),
          }
        );
  
        const responseData = await response.json();
        setFlaskResult(responseData.message);
      } catch (error) {
        setFlaskResult(error.message);
      }
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If this helps anyone:

I overloaded the API call:

  const onCallEndpoint = async (props) => {
    const {endpoint} = props;
    setFlaskResult(null);
    try {
        const flow = reactFlowInstance.toObject();
        const token = await getAccessTokenSilently();
        const response = await fetch(
          `${serverUrl}/${props.endpoint}`,
          {
            headers: {
             'Content-Type': 'application/json',
              Authorization: `Bearer ${token}`,
            },
            method: 'POST',
            body:JSON.stringify(flow),
          }
        );
  
        const responseData = await response.json();
        
        setFlaskResult(responseData.message);
      } catch (error) {
        setFlaskResult(error.message);
      }
};

Then in popover I did the following:

 const popover = (
    <Popover id="popover-basic">
      <Popover.Header as="h3">Num Nodes</Popover.Header>
      <Popover.Body>
      The number of nodes is: {(flaskResult) ? flaskResult:"Computing!"}.
        
      </Popover.Body>
    </Popover>
  );
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