Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda