I have MyComponent using setData () to write data in state and getData () to read state. Is this the best reagent practice? It works great for me, but not sure what I'm doing is the easiest way, please advise
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
data: []
};
}
setData(){
this.setState({data:"123"});
}
getData() {
console.log(this.state.data);
}
componentDidMount() {
this.setData();
}
componentDidUpdate() {
this.getData();
}
render() {
return (
<div>
{this.state.data} // 123 OK !!!
</div>
);
}
}
source
share