title: How to write and read component state in React?
body: <p>I have MyComponent using setData() to write data in state and getData() to read the state. Is it best practice in React ? It works fine for me but not sure if what I am doing is the simplest possible way, please advice</p>
  
  <pre><code>class MyComponent extends Component {
    constructor(props) {
      super(props);
      this.state = {
        data: []
      };
  
    }
  
    setData(){    
      this.setState({data:"123"});   
    }
  
    getData() {
      console.log(this.state.data); // 123 OK !!!   
    }
  
    componentDidMount() {    
      this.setData();  
  
    }
    componentDidUpdate() { 
      this.getData();
    }
    render() {
      return (                     
         &lt;div&gt;           
            {this.state.data} // 123 OK !!!
         &lt;/div&gt;                        
      );
    }
  }
  </code></pre>