UPDATED:
I have a question for you. In my react app I have basic state like below.
constructor(props){
super(props);
this.state = {
title: "Hello";
}
}
And think that I have button and onClick event I add new dynamic key to state. Let's say [key] = "newValue" and "val"= 1.
onClickEvent(){
let splitArray = value.split('_');
let key = splitArray[0];
let val = splitArray[1];
this.setState({
[key]: val
});
}
render(){
return(
<div>
<button onClick={this.onClickEvent}>Click</button>
<input value={} />
</div>
);
}
Everything is Ok until now. But I don't know how to use this dynamically created new state property which in case my example [key].
In render method, my input value is empty because I don't have value in the state.
After the click event, I add new state key and I want that my render like this.
render(){
return(
<div>
<button onClick={this.onClickEvent}>Click</button>
<input value={this.state.[key]} />
</div>
);
}
Is there any life cycle method of react component, detects the added new state property ? Because I want to use that newly created state property.
I hope my question is clear.
Store the key in your state too.
Then you can implement
componentDidUpdateand use the key to read the new state value.