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

138
Vistas
Any reason why only the last clicked img file is returning?
    const [photo, setPhoto] = React.useState([]);

    const addPhoto = async (e) => {
      await setPhoto(e.target.files);
    };

    const returnPhoto = () => {
        if (photo.length > 0) {
         for (let i = 0; i < photo.length; i++)  {
             return(
                <div style={{background: '#303030', display: 'inline-flex'}}>
                    <img alt='pic' style={{maxHeight: '10em', maxWidth: '10em'}} src={URL.createObjectURL(photo[i])}/>
                </div>
             )}}}

Does anyone see what I did wrong? I'm trying to return a useState's current selected files... I assume I should alter the addPhoto() function but so far I don't know how.

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

0

Your main problem is that putting return in a regular for loop exits the loop on the first iteration. You'd want to use something like map() to return an array of JSX elements.

Because the files property is a FileList, you won't be able to directly use map() but you can convert it to an array.

You could also optimise this by memo-ising the created URLs

const [ photos, setPhotos ] = useState([]); // really think this should be plural

const addPhoto = (e) => { // no need for async
  setPhotos(e.target.files);
};

// photos is a FileList so convert to an array
const photoUrls = useMemo(() =>
  Array.from(photos, URL.createObjectURL), [ photos ]);

useEffect(() => () => {
  // cleanup
  photoUrls.forEach(URL.revokeObjectURL);
}, [ photoUrls ]);

const returnPhoto = () => photoUrls.map(url => (
  <div style={{background: "#303030", display: "inline-flex"}}>
    <img
      src={url}
      alt="pic"
      style={{maxHeight: "10em", maxWidth: "10em"}}
    />
  </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