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

181
Vistas
React storing state in an element variable

I want to know how, if possible, I can store a state in an element that is stored in a variable.

This is my simplified code:

export class DashboardLists extends Component{
constructor(props){
    super(props);

    this.state = {
        listCount: 0,
        ...
    }

    ...

    this.headerLeft = <h2 style={{marginTop: "0px"}}>Lists {this.state.listCount}</h2>;

    ...
}

render(){
    return(
          <div>
              ...
              {this.headerLeft}
              ...
          </div>
    );
}
}

When I call state change, the value in the element doesn't display the updated state value.

It is necessary for me to store the element in a variable, because in my full code I must pass the element as a prop to another element.

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

0

Remove headerLeft from constructor. Create a dedicated method in class, something like getHeaderLeft() {return <h2 style={{marginTop: "0px"}}>Lists {this.state.listCount}</h2>;} and call it in render method {this.getHeaderLeft()}

- Konstantin Samarin

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