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

87
Vistas
Display image from .map array (React)

        setForecast(res2.data.list.map(item => [
          <div className="each_data_point">
            <li className="date_time" key={item.dt_txt}>{`Date & Time: ${item.dt_txt}`}</li>,
            <img className="icon" key={item.weather[0].icon}>{`https://openweathermap.org/img/w/${item.weather[0].icon}.png`}</img>,
            <li className="main_temp" key={item.main.temp}>{`Temp: ${item.main.temp}`}</li>
          </div>
        ]
        ))
        
        
        ......
        
        
        return (
        <div>
        <div className="five_day_forecast">
            <div className="temp_body">
            
            // Display here with Date/Time & Main Temp.
                <p>{forecast}</p>

            </div>
            </div>

Is it possible to render an image from array.map() within React. When I run this, the DOM clears completely to white screen.

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

0

Using src in the img tag to render image

 <img className="icon" key={item.weather[0].icon} src={`https://openweathermap.org/img/w/${item.weather[0].icon}.png`} />

And put the key in div instead in the children.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Not sure why you are doing it, but setting JSX in your state seems strange.

Try something like this, assuming res2.data.list has the list of values.

<div className="five_day_forecast">
        <div className="temp_body">
          // Display here with Date/Time & Main Temp.
          <p>
            {res2.data.list && res2.data.list.length >0 && res2.data.list.map(item => [
              <div className="each_data_point">
                <li className="date_time" key={item.dt_txt}>{`Date & Time: ${
                  item.dt_txt
                }`}</li>
                ,
                <img
                  className="icon"
                  key={item.weather[0].icon}
                >{`https://openweathermap.org/img/w/${
                  item.weather[0].icon
                }.png`}</img>
                ,
                <li className="main_temp" key={item.main.temp}>{`Temp: ${
                  item.main.temp
                }`}</li>
              </div>
            ])}
          </p>
        </div>
      </div>
about 4 years ago · Juan Pablo Isaza Denunciar

0

Putting markup in state is a weird anti-pattern. State is really there to just store data not markup. You can use a function to create the JSX from mapping over the data.

You should be adding the source of the image to the src attribute of the img element.

Keys should be added to parent elements.

const imgSrc = `https://openweathermap.org/img/w/${item.weather[0].icon}.png`;
<img src={imgSrc} />

// `fetch` your data, and add it to state
setForecast(res2.data.list);

// `map` over the data and create the markup
function getMarkup() {

  const imgSrc = `https://openweathermap.org/img/w/${item.weather[0].icon}.png`;

  return forecast.map(item => {
    <div className="each_data_point" key={item.dt_txt}>
      <li className="date_time">{`Date & Time: ${item.dt_txt}`}</li>,
      <img src={imgSrc} className="icon" />,
      <li className="main_temp">{`Temp: ${item.main.temp}`}</li>
    </div>
  });

}

return (
  <div>
    <div className="five_day_forecast">
      <div className="temp_body">

        // Call the function to return the markup
        // build from state
        {getMarkup()}
    </div>
  </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