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