title: Asynchronous xmlhttp request in react
body: <p>I am trying to implement asynchronous XMLHttpRequest in react.
  Here is my attempt:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var xhr = new XMLHttpRequest();
  var json_obj, status = false;
  xhr.open("GET", "https://jsonplaceholder.typicode.com/photos/", true);
  xhr.onload = function (e) {
    if (xhr.readyState === 4) {
      if (xhr.status === 200) {
        json_obj = xhr.responseText;
        status = true;
      } else {
        console.error(xhr.statusText);
      }
    }
  };
  xhr.onerror = function (e) {
    console.error(xhr.statusText);
  };
  xhr.send(null);
  
  class Welcome extends React.Component {
    render() {
      return (
        &lt;div&gt;
            &lt;img src= {status ?  json_obj[0].url : 'loading...'}&gt;&lt;/img&gt;
        &lt;/div&gt;
      );
    }
  }
  ReactDOM.render(
     &lt;Welcome/&gt;,
     document.getElementById('root')
  );</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"&gt;&lt;/script&gt;
  &lt;div id="root"&gt;&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I have been thinking about adding listener to it but i don't know how to do it.</p>
  
  <p>Overall i am having problem with an update after async XMLHttpRequest loads and returns value.</p>