2016-08-14 19 views
0

我嘗試從txt文件中讀取數據,並將其傳遞到初始內容的內部文本中。這是我的組件一切工作正常,除了{} this.state.innerText和getinitialstate功能雖然React textarea intialstate

var TxtAreaControl = React.createClass({ 
    getInitialState: function() { 
     var data = initLoadNotes(); 
     return { 
      innerText: data 
     } 
    }, 
    saveNotes: function(){ 
     saveChanges(); 
    }, 
    render: function() { 
     return (
     <div> 
      <textarea id = "txtarea" onBlur={this.saveNotes} rows = "11" columns = "30" > 
       {this.state.innerText} 
      </textarea> 
     </div> 
     ); 
    } 
}); 

和initLoadFunction

function initLoadNotes(){ 
    fs.readFile('./notes.txt', 'utf-8', function (err, data) { 
      if(err){ 
       alert("An error ocurred reading the file :" + err.message); 
       return; 
      } 
      // Change how to handle the file content 
      //document.getElementById('txtarea').textContent = data; 
      return data; 
    }); 
} 

我如何做到這一點?

回答

0

問題是我讀文件異步。我將readfile更改爲readfilesync並修復了