title: Setting State From File Contents
body: <p>I'm trying to set my state variable, to the contents read in from a text file.</p>
  <p>Whenever I try to set my state variable within the <code>r.onload</code> anonymous function, I get the error that the <code>setState()</code> method is not defined.</p>
  <p>Which I'm guessing is because of JavaScript's lexical scoping.</p>
  <p>I tried binding &quot;this&quot; to a &quot;that&quot; variable and then passing it inside of the <code>r.onload</code> anon function and call <code>that.setState()</code>, but I still get the same result.</p>
  <p>How can I do my <code>r.onload</code> anonymous function, and set my state variable once the file contents are read?</p>
  <p>React Component:</p>
  <pre><code>&lt;input type=&quot;file&quot; onChange = {this.file} /&gt;
  </code></pre>
  <p>React Method:</p>
  <pre><code>file:  function(evt) {
      //Retrieve the first (and only!) File from the FileList object
      var f = evt.target.files[0]; 
      
      if (f) {
        var r = new FileReader();
        r.onload = function(e){ 
            var contents = e.target.result;
            this.setState({ text: contents });
            alert(contents);  
        } 
        r.readAsText(f);
        } else{ 
        alert(&quot;Failed to load file&quot;);
      }
    }
  </code></pre>