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

193
Visualizações
How to display images from a JSON URL array in React

I have converted a JSON endpoint into a JavaScript array and I've mapped through it to get the key values I need. 3 out of 4 are text values but the first one is an image and it just displays the URL link. I have tried to map through the same array and display just the images and it works but then I cannot merge the two elements into one div.

The code:

export default function Pokes() {

  const [pokemonData, setPokemonData] = React.useState({});

  React.useEffect(() => {
    fetch(
      "https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json"
    )
      .then((res) => res.json())
      .then((data) => setPokemonData(data.pokemon));
  }, []);

  const allPokes = pokemonData;
  const pokemons = Object.values(allPokes);
  
  const pokesData = pokemons.map(pokemon => `${pokemon.img} ${pokemon.num} ${pokemon.name} ${pokemon.type}`);

let renderedOutput = pokesData.map(item => <div className="infodiv" style={{ flex: 1, flexBasis: "33%" }}> {item} </div>)


 return (
        <main>
          <div>
         
            <div style={{ display: "flex", flexWrap: "wrap" }}>{renderedOutput}</div>
           
          </div>
        </main>
      );

}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

const pokesData = pokemons.map(pokemon => `${pokemon.img} ${pokemon.num} ${pokemon.name} ${pokemon.type}`)

This line of code would return "image url number name", what you actually want is the real image which requires the use of the img HTML tag. Implementing this with your code, it would become:

export default function Pokes() {

  const [pokemonData, setPokemonData] = React.useState({});

  React.useEffect(() => {
    fetch(
      "https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json"
    )
      .then((res) => res.json())
      .then((data) => setPokemonData(data.pokemon));
  }, []);

  const allPokes = pokemonData;
  const pokemons = Object.values(allPokes);

  let renderedOutput = pokemons.map(pokemon => <div className="infodiv" style={{ flex: 1, flexBasis: "33%" }}>  <img src={pokemon.img} /> {pokemon.num} {pokemon.name}  </div>)
// Note the code change above ^^^

 return (
        <main>
          <div>
         
            <div style={{ display: "flex", flexWrap: "wrap" }}>{renderedOutput}</div>
           
          </div>
        </main>
      );

}
about 4 years ago · Juan Pablo Isaza Relatório

0

Here is the solution if that is what you are looking after. Here is codesandbox for below code;

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

const URI =
  "https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json";

const App = () => {
  const [values, setValues] = useState([]);

  const getPokomonGo = useCallback(async () => {
    try {
      const { data } = await axios.get(URI);
      if (data) setValues(data?.pokemon);
    } catch (err) {
      console.log({ err });
    }
  }, []);

  useEffect(() => {
    getPokomonGo();
  }, [getPokomonGo]);

  return (
    <div className="App">
      <h1>Pokemon Images</h1>
      {values &&
        values.map(({ num, name, img }) => (
          <img src={img} alt={name} key={num} />
        ))}
    </div>
  );
};

export default App;

about 4 years ago · Juan Pablo Isaza Relatório

0

<img src={{item.img}} alt="Lamp" width="100" height="100">

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