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

345
Views
Reaccionar: ¿cómo establecer `dangerouslySetInnerHTML` desde el estado del componente?

Soy nuevo en React y estoy trabajando en un componente ya existente que parece un poco antiguo.

Tengo un elemento de etiqueta que quiero configurar dinámicamente, según el estado. A este efecto tengo:

 export default class Address extends React.Component { // Props and other handlers componentTitle() { if (this.state.isPostal) { return this.state.labels.PostalAddressLabel } else { return this.state.labels.StreetAddressLabel } }; render() { return ( <div className="block"> <div className="grid-x"> <div className="input-field cell"> <label dangerouslySetInnerHTML={{ __html: componentTitle() }} /> // Other elements } }

Sin embargo, esto no funciona. me sale el error

Error de referencia no detectado: el título del componente no está definido

He probado varias combinaciones de esto, incluyendo

  • Usando un var const compTitle = componentTitle() {

  • colocando la función dentro de la función render()

  • Y eliminando la función por completo y poniendo la condición if/else en la función render(), por ejemplo:

     if (this.state.isPostal) { const compTitle = this.state.labels.PostalAddressLabel } else { const compTitle = this.state.labels.StreetAddressLabel } return ( <div className="block"> <div className="grid-x"> <div className="input-field cell"> <label dangerouslySetInnerHTML={{ __html: compTitle }}

Pero simplemente no puedo hacer que funcione.

¿Alguien podría indicarme la dirección correcta?

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

0

En este código:

 if (this.state.isPostal) { const compTitle = this.state.labels.PostalAddressLabel } else { const compTitle = this.state.labels.StreetAddressLabel }

Usted, la variable compTitle solo vive dentro del alcance definido, que se encuentra entre las llaves. Entonces, después de la declaración if , esa variable ya no existe.

Sospecho que quieres algo como esto:

 const compTitle = this.state.isPostal ? this.state.labels.PostalAddressLabel : this.state.labels.StreetAddressLabel return ( <div className="block"> <div className="grid-x"> <div className="input-field cell"> <label>{compTitle}</label>
about 4 years ago · Juan Pablo Isaza Report

0

La forma más fácil de hacer esto es usando el operador ternario.

 export default class Address extends React.Component { // Props and other handlers render() { const { isPostal, labels } = this.state; const compTitle = isPostal ? labels.PostalAddressLabel : labels.StreetAddressLabel; return ( <div className="block"> <div className="grid-x"> <div className="input-field cell"> <label dangerouslySetInnerHTML={{ __html: compTitle }} /> // Other elements ) } }
about 4 years ago · Juan Pablo Isaza Report

0

use esta palabra clave this.componentTitle()

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!