title: Can I put ajax in React component constructor?
body: <pre><code>import React from 'react';
  
  class AjaxInConstructor extends React.Component {
    constructor() {
      super();
      this.state = { name: '', age: '' };
      this.loadData().then((data) =&gt; {
        this.setState(data);
      });
    }
    // simulate the AJAX (network I/O)
    public loadData() {
      return new Promise((resolve, reject) =&gt; {
        setTimeout(() =&gt; {
          resolve({
            name: 'slideshowp2',
            age: 123,
          });
        }, 2000);
      });
    }
  
    public render() {
      const { name, age } = this.state;
      return (
        &lt;div&gt;
          &lt;p&gt;Can I init component state async?&lt;/p&gt;
          &lt;p&gt;name: {name}&lt;/p&gt;
          &lt;p&gt;age: {age}&lt;/p&gt;
        &lt;/div&gt;
      );
    }
  }
  
  ReactDOM.render(&lt;AjaxInConstructor /&gt;, document.body);
  </code></pre>
  <p>Above is my demo code. I know people always put ajax in <code>componentDidMount</code> or <code>componentWillMount</code> lifecycle.</p>
  <p>But this case also works.</p>
  <p>In chrome <code>console</code>, <code>React</code> throw no error and waring. So, My Question is usage like this is completely correct ? Is there have some error?</p>