当前位置: 首页 > 知识库问答 >
问题:

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

凤安然
2023-03-14
 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]} />
    }
  }
}

共有1个答案

魏宏邈
2023-03-14

用箭头更改回调函数:

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

主函数有.bind,但是在这个函数中,对.then和foreach方法使用常规回调函数。这些函数创建了它们自己的作用域,您正在丢失这个。使用箭头函数,您不会松散这个'作用域。

额外的信息:你可以使用一个箭头函数为你的getList函数也。通过这种方式,您将不需要在构造函数中绑定它。

 类似资料:
  • 问题内容: 我试图在ajax回调从REST api接收数据后设置组件的setState。这是我的组件构造函数代码 然后,我有一个如下所示的方法。 现在,这是我执行getAJAX请求的getPosts函数。 我想设置状态,但出现以下错误。 不太清楚是什么原因造成的。如果有人指出我正确的方向,那将真的很有帮助。提前致谢。 问题答案: 还绑定回调函数,以便回调内部指向React Component的上下

  • 我目前正在尝试在reactJS中构建一个小型天气应用程序(freecodecamp的应用程序) 目前我收到错误:“UncaughtTypeError:无法读取未定义的属性'setState'” 以下是代码笔的链接:http://codepen.io/rasmus/pen/aNGRJm 下面是代码,我想这就是问题所在: url不是问题所在。我可以将接收到的数据记录到控制台。 我想这是因为的范围。。

  • 我有一个简单的代码,但我收到(有时)错误的结果,这是因为nodejsasync,我知道。最后,我需要更新“this.setState({active:options})”。因此,为了解决NodeJS的异步问题,我使用了require(“异步”)库。但是现在我的简单代码变得相当复杂,结果也不是100%好。 同样在“函数道路”上,当我使用“this.setState({actove:options})

  • 请注意,我在react ProductScreen.js中呈现动态数据时有问题 注意,...数据在product.js中呈现得很好,但相同的数据在productscreen.js中不会呈现,因为productscreen.js链接是通过“id”呈现的。 谢谢app.js 导入“./homescreen.css”;从'../Components/Product'导入产品从'React'导入{useE

  • 问题内容: 我是Reactjs的新手。我正在尝试做一个非常简单的事情:当用户在文本区域内更改文本时,在render函数中更新div。有什么建议? 问题答案: 您应该绑定该函数。您收到此错误的原因是,在handleChange函数中,键盘操作未引用React类的上下文,因此您需要绑定该函数。 看到这个答案