【发布时间】:2018-12-05 02:06:39
【问题描述】:
我正在尝试设置我的 FileList 组件的状态,但我不断收到错误消息
未处理的拒绝 (TypeError):无法读取未定义的属性“setState” 从第 43 行开始 - this.setState({list: entries});
按照网上的建议,我已经将函数绑定到这个,有没有人知道如何让它工作
export default class FileList extends Component {
constructor(props) {
super(props);
this.getList = this.getList.bind(this);
this.renderFiles = this.renderFiles.bind(this);
this.state = {
zipFile: props.zipFile,
uploadPressed: props.uploadPressed,
list: []
}
}
getList() {
var entries = new Array;
let zip = new JSZip();
JSZip.loadAsync(this.state.zipFile).then(function (zip) {
zip.forEach(function (zipEntry) {
if(zipEntry.split('/')[1] === "color"){
if(zipEntry.endsWith('.png')) {
entries.push(zipEntry.split('/').pop());
} else if (zipEntry.endsWith('/')) {
} else {
}
} else if(zipEntry.split('/')[1] === "mono") {
if(zipEntry.endsWith('.png')) {
entries.push(zipEntry.split('/').pop());
} else if (zipEntry.endsWith('/')) {
} else {
}
} else if(zipEntry.endsWith('.sslt')) {
} else {
}
});
alert(entries[0]);
this.setState({list: entries});
});
}
render() {
return <div className="file-list">
<div className="list-zip" >
<div className="list-zip-name">
{this.state.zipFile.name}
</div>
<div className="list-zip-size">
{this.state.zipFile.size} Bytes
</div>
<div className="list-zip-x" >
<button className="x-button" onClick={close}>X</button>
</div>
</div>
<hr className="hr"/>
{this.renderFiles()}
</div>
}
renderFiles() {
if(this.state.uploadPressed === true) {
this.getList();
return <File fileName={this.state.list[0]} />
}
}
}
【问题讨论】:
标签: javascript html reactjs binding this