Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
React: Async/Await function returns an undefined array

The code below where I was using an async function was working fine and it would return an array from my Heroku Database.

However, as soon as I tried to map over the array toDos, it would return toDos as undefined and therefore unable to map over the array.

I've tried to look through some previous answers, but they weren't specific to my situation.

Please see below

import "./App.css";
import React, { useState, useEffect } from "react";

import Input from "../Input/Input";
import List from "../List/List";

function App() {
      const [inputValue, setInputValue] = useState("");
      const [toDos, setToDos] = useState([]);

      useEffect(() => {
            async function getToDos() {
                  try {
                        const response = await fetch(
                              "http://localhost:5000/todo"
                        );
                        const data = await response.json();
                        setToDos(data);
                        console.log(data);
                  } catch (error) {
                        console.error(error.message);
                  }
            }
            getToDos();
      }, []);

      console.log(toDos.payload);
      console.log(toDos);
      const arrayToMap = toDos.payload;

      function getValue(text) {
            setInputValue(text);
            console.log(inputValue);
      }

      return (
            <div className="App">
                  <Input getValue={getValue} />
                  <List arrayToMap={arrayToMap} />
            </div>
      );
}

export default App;

Anything I can do to resolve it? Thank you in advance.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda