在我的react组件中,我有一个文件输入:
<input type="file" onChange={this.onFileChange.bind(this)} />`
我的onFileChange
是:
onFileChange(e) {
let file = e.target.files[0];
this.setState(() => ({ file: e.target.files[0] })); //doesnt work
// this.setState(() => ({ file })); //works
// this.setState({ file: e.target.files[0] }); //works
}
第一种设置状态的方法失败并出现错误:
Cannot read property 'files' of null
React 还给出了以下警告:
This synthetic event is reused for performance reasons. If you're
seeing this, you're accessing the property 'target' on a
released/nullified synthetic event
但是最后两种设置状态的方法没有给出错误或警告。为什么会这样?