我尝试从txt文件中读取并将其传递给内部文本以获取初始内容。这是我的组件一切正常,除了{this.state.innerText}和getinitialstate函数
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 :(得分:0)
问题是我读取文件异步。我将readfile更改为readfilesync并修复了