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

186
Visualizações
I can't access the data stored in my sessionDates array in my DateList function

I can't access the data stored in my sessionDates array in my DateList function

this is the code from my index.js including the api call

import React from "react";
import ReactDOM from "react-dom";
const availableDates = [];
const available = [];
const sessionDates = [];

function getDates(e){
  getDate(apiUrl);
}

const getDate = async (apiUrl)=>{
  const res = await fetch(apiUrl)
  try {
    const data = await res.json();
    availableDates.push(data);
    availableDates.map((datee) => {
      datee.map((session) => {
        sessionDates.push(session.date.slice(0, 10));
      });
    });
    return sessionDates;
  }  catch(error) {
    console.log("error", error);
  }
}
window.addEventListener('load', getDates);

function DateList(){
  return <section>{sessionDates[0]}</section>;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

While you are setting sessionDates based on the api call, there is nothing in your code that prompts DateList to re-render when sessionDates changes. As such, it will only display the initial value of sessionDates, for which sessionDates[0] is undefined.

Instead, I recommend that you use the useEffect hook to trigger your api call and implement sessionDates using useState.

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