3

I am having an issue where I can't seem to be able to display the pokemon images on my react front end, this is the api: https://pokeapi.co/

import React, { Component } from 'react';

class Card extends Component {
    state = {
        name: "",
        imageUrl: "",
        pokemonIndex: "",
    }
    componentDidMount() {
        const {name, url} = this.props;
        const pokemonIndex = url.split('/')[url.split('/').length - 2];
        const imageUrl = `http://pokeapi.co/media/sprites/pokemon/${pokemonIndex}.png`
        this.setState({name, imageUrl, pokemonIndex})
    }
    render() {
        
       
        
        return (
            <div className="card" >
                <img src= {this.state.imageUrl}/>
                <h3> {this.state.name} </h3>
                
            </div>
        );
    }
}

export default Card;

I have also attached a screenshot of the front end.enter image description here

Naramsim
  • 8,059
  • 6
  • 35
  • 43
RMP1992
  • 55
  • 1
  • 1
  • 9
  • Does this answer your question? [Third party image acces issue 403 error](https://stackoverflow.com/questions/11503374/third-party-image-acces-issue-403-error) – Joe Sep 19 '21 at 01:00

4 Answers4

8

Looking at the documentation and JSON file from API, I think I found your problem. You are using the wrong img link. The correct format is: https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemonIndex}.png

For exemple:

<img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png" />
Erick Silva
  • 338
  • 1
  • 5
2

@erick-silva answer is not complete and prone to error.

The correct way to fetch an image for a specific pokemon is:

  1. Fetch from PokeAPI the info for that pokemon, say bulbasaur. -> GET https://pokeapi.co/api/v2/pokemon/bulbasaur
  2. Parse the returned JSON for the property .sprites, select the version we'd like to use and the variety for the sprite, say Pokemon Crystal front: .sprites.versions["generation-ii"].crystal.front_default
  3. Use the provided link and load the image: https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/versions/generation-ii/crystal/1.png

Using this method will ensure that you will always fetch the correct image in case the ids change or the name of the image isn't the id of the pokemon.

Naramsim
  • 8,059
  • 6
  • 35
  • 43
1

Currently pokemon images are not loaded as there are deleted from it's github repo.

You can find the real location of the images inside sprites folder in PokeAPI GitHub.

To get the image, there is a workaround,

https://unpkg.com/pokeapi-sprites@2.0.2/sprites/pokemon/other/dream-world/1.svg

here 1.svg should be replaced by the pokemon's id

You can find more info here

Anand Raja
  • 2,676
  • 1
  • 30
  • 35
0

For pokeapi the correct URI format is as follows.

https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/imageName.png
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 29 '22 at 05:59