Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
¿Alguna razón por la que solo regresa el último archivo img en el que se hizo clic?
 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> )}}}

¿Alguien ve lo que hice mal? Estoy tratando de devolver los archivos seleccionados actuales de useState... Supongo que debería modificar la función addPhoto() pero hasta ahora no sé cómo.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Su principal problema es que poner return en un bucle for regular sale del bucle en la primera iteración. Querría usar algo como map() para devolver una matriz de elementos JSX.

Debido a que la propiedad de files es una Lista de archivos, no podrá usar map() directamente, pero puede convertirlo en una matriz.

También puede optimizar esto memorizando las URL creadas

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!