简体   繁体   English

无法读取未定义REACT的setState属性

[英]Cannot read property of setState of undefined REACT

I'm trying to set the state of my FileList component, but I keep getting an error 我正在尝试设置FileList组件的状态,但是我一直收到错误消息

Unhandled Rejection (TypeError): Cannot read property 'setState' of undefined from line 43 - this.setState({list: entries}); 未处理的拒绝(TypeError):无法从第43行读取未定义的属性“ setState”-this.setState({list:entrys});

I've bound the function to this, as suggested online, does anyone know how to get this to work 正如在线建议的那样,我已将此功能绑定到此,是否有人知道如何使其正常工作?

 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]} />
    }
  }
}

Change your callback functions with arrow ones: 用箭头键更改回调函数:

....
JSZip.loadAsync(this.state.zipFile).then( zip => {
zip.forEach( zipEntry => {
    if(zipEntry.split('/')[1] === "color"){
...

You have a .bind for your main function but inside this function you are using regular callback functions for .then and forEach methods. 您的主函数有一个.bind ,但是在此函数内部,您正在对.thenforEach方法使用常规回调函数。 Those functions create their own scope and you are loosing this . 这些功能创建了自己的作用域,您对此失去this With arrow functions you won't loose this ' scope. 使用箭头功能,您将不会失去this范围。

Bonus info: You can use an arrow function for your getList function also. 奖励信息:您还可以将箭头功能用于getList函数。 With this way you won't need to bind it in your constructor. 这样,您就不需要将其绑定到构造函数中。

A part from the fact that you should be using arrow functions to avoid loosing the current context as already reported by other users you have another error that will raise as soon as you fix the previous. 您应该使用箭头功能来避免失去其他用户已经报告的当前上下文这一事实的一部分,您还有另一个错误将在修复前一个错误后立即提出。

you're calling setState inside the render method, this will create an infinite loop, to avoid it just put the getList method inside the componentDidMount hook 您在render方法内部调用setState ,这将创建一个无限循环,以避免将其仅将getList方法放在componentDidMount钩子内

This is a scoping problem. 这是一个范围界定问题。 Your callback function doesn't have access to the component's this . 您的回调函数无权访问组件的this You need to use .bind(this) or an arrow operator ( () => {} ) to access this within your function 您需要使用.bind(this)或箭头运算符( () => {} )才能在函数中访问this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM