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
React When i am trying to get into a nested object the fetch throws me an undefined error
import React from 'react'
import { useEffect, useState } from 'react'



function PokemonCard() {
const [pokemon, setPokemon] = useState([])
useEffect(() => {
  fetch('https://pokeapi.co/api/v2/pokemon/charizard')
  .then(res => res.json())
  .then(setPokemon)
},[])
const pokemonType = pokemon.types
console.log(pokemonType)

//HERE I GET THE ERROR
pokemonType.map(x => console.log(x.type.name))
return (
<div>
    <h1>{pokemon.name}</h1>
      {/* <img src={pokemon.sprites.front_default}></img> */}
      {/* <h2>Type: {pokemonType}</h2> */}
      <h2>Height: {pokemon.height}</h2>
      <h2>Weight: {pokemon.weight}</h2>
</div>
)
}


export default PokemonCard;

this is the link for the api

https://pokeapi.co/api/v2/pokemon/charizard

when i try to get into the pokemon.types.type.name i get a error saying Uncaught TypeError: Cannot read properties of undefined (reading 'map') at PokemonCard (PokemonCard.js:16:1) at renderWithHooks (react-dom.development.j

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

That is due to pokemonType is undefined initially. Try using optional chaining operator:

pokemonType?.map((x) => console.log(x.type.name));

Answer for what you tried in the comment:

const pokemonType = pokemon.types?.[0]?.type.name;

If you wanted to get the types rendered then use this:

function PokemonCard() {
  const [pokemon, setPokemon] = React.useState([]);
  React.useEffect(() => {
    fetch("https://pokeapi.co/api/v2/pokemon/charizard")
      .then((res) => res.json())
      .then(setPokemon);
  }, []);

  function capitalizeFirstLetter(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
  }

  return (
    <div>
      <h1>{pokemon.name}</h1>
      {pokemon.types &&
        pokemon.types.map(({ type: { name } }) => (
          <p key={name}>{capitalizeFirstLetter(name)}</p>
        ))}
      <h2>Height: {pokemon.height}</h2>
      <h2>Weight: {pokemon.weight}</h2>
    </div>
  );
}

ReactDOM.render(<PokemonCard />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

map over the types property in the JSX. Note that the objects in the types array doesn't hold height or weight properties so I'm assuming you wanted to add the height and weight from the pokemon object.

If you want to make the name uppercase add a class to the element that holds the name, and use CSS to make it uppercase. If you meant only to capitalise the first letter use .pokemonName { text-transform: capitalize; } instead.

const { useEffect, useState } = React;

function PokemonCard() {

  const [ pokemon, setPokemon ] = useState();

  useEffect(() => {
    fetch('https://pokeapi.co/api/v2/pokemon/charizard')
      .then(res => res.json())
      .then(setPokemon);
  }, [])

  if (!pokemon) return <div>Loading pokemon data</div>;

  return (
    <div>
      {pokemon.types.map(({ id, type }) => {
        return (
          <div key={id}>
            <h3 className="pokemonName">
              {type.name}
            </h3>
            <p>Height: {pokemon.height}</p>
            <p>Weight: {pokemon.weight}</p>
          </div>
        );
      })}
    </div>
  );

}

ReactDOM.render(
  <PokemonCard />,
  document.getElementById('react')
);
.pokemonName { text-transform: uppercase; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

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