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

178
Views
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 answers
Answer question

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 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!