set state before new render react code example
Example: react set state before render
componentDidMount() {
this.setState({
thing: [
{
status: "running",
test: "testing"
}
]
});
}
render() {
return (
<div>
{this.state.thing.length > 0? <h1 className="mt-5 mb-5">{ this.state.thing[0].status }</h1>: null
}
<button className="mt-5" onClick={ this.handleUpdate } value="not running">
Click to change
</button>
</div>
);
}