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

125
Vistas
undefined is not an object axios

I'm getting this error when i try to use GET method in AXIOS. At the bottom I put a code snippet without style. Wants to grab data from the API using AXIOS. I'm new and don't quite know how to do this correctly.

undefined is not an object (evaluating 'data.map')

code:

import React, {useState, useEffect} from 'react';
import axios from 'axios';

const HomeScreen = ({navigation}) => {

    const [categoryIndex, setCategoryIndex, data, setData] = useState([])

    useEffect(() => {
        const fetchData = async () => {
            const result = await axios(
                'https://hn.algolia.com/api/v1/search?query=redux',
            );
            setData(result.data);
        };
        fetchData();
    }, []);

    return (
        <SafeAreaView
            <ul>
                {data.map(item => (
                    <li key={item.objectID}>
                        <a href={item.url}>{item.title}</a>
                    </li>
                ))}
            </ul>
        </SafeAreaView>
    );
};

export default HomeScreen;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

  • Hi, your data is not like you expected. There is also "hits" variable that exists, I think you want to show that data.
  • In the first render, it's trying to map your data but data was not filled when trying to map it.
  • Also, you should be re-examined "useState" usage.
  • And you need to check if data exist before the map it (data?.hits).
  • And you forgot to close the SafeAreaViewtag.

https://reactjs.org/docs/hooks-state.html

https://codesandbox.io/s/ancient-fast-pdqhy?file=/src/TestApp.jsx

If you paste this it will work correctly:

import React, { useState, useEffect } from "react";
import axios from "axios";

const HomeScreen = ({ navigation }) => {
  const [data, setData] = useState([]);
  const [categoryIndex, setCategoryIndex] = useState([]);

  useEffect(() => {
    const fetchData = async () => {
      const result = await axios(
        "https://hn.algolia.com/api/v1/search?query=redux"
      );
      console.log(result);
      setData(result.data);
    };
    fetchData();
  }, []);

  return (
    <SafeAreaView>
      <ul>
        {data?.hits &&
          data.hits.map((item) => (
            <li key={item.objectID}>
              <a href={item.url}>{item.title}</a>
            </li>
          ))}
      </ul>
    </SafeAreaView>
  );
};

export default HomeScreen;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar
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