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

573
Views
¿Por qué document.getElementById() devuelve nulo en React aunque el elemento está en React?

Tengo este código dentro de un componente de clase React y cuando intento llamar a document.getElementById(photo._id) sigue devolviendo nulo, aunque cuando probé console.log(photo._id) en ambas líneas, la línea en el renderizado se imprime primero. en la consola, ¿entonces se supone que la identificación debe estar allí?

 componentDidMount() { this.props.location.state.product.photos.map((photo) => { return axios .get(`${SERVER_HOST}/products/photo/${photo.filename}`) .then((res) => { if (res.data) { if (res.data.errorMessage) { console.log(res.data.errorMessage); } else { console.log(photo._id); console.log(document.getElementById(photo._id)); } } else { console.log("Record not found"); } }); }); } render() { return ( {this.props.location.state.product.photos.map((photo) => { { console.log(photo._id); } <img key={photo._id} id={photo._id} />; })} ) }

aqui una foto de la consola

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

0

No está devolviendo el elemento de imagen en la función de mapa. Por lo tanto, no está montado en el árbol dom y no puede acceder a él usando document.getElementById()

 render() { return ( {this.props.location.state.product.photos.map((photo) => { { console.log(photo._id); } {/** return keyword is missing here **/} return <img key={photo._id} id={photo._id} />; })} ) }
about 4 years ago · Juan Pablo Isaza Report

0

Reemplace el cuerpo de su función de procesamiento con el siguiente código.

 return this.props?.location?.state?.product?.photos?.map((photo) => <img key={photo._id} id={photo._id} src={...} />;
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!