小编mr *_*est的帖子

为什么我会收到“通过嵌套函数中的 this 对类字段的潜在无效引用访问”错误

在 vanilla JS 中,我的代码可以正常工作。对于这种情况,我想组件化我的Wall类,该类应该在用户上传的浏览器中显示图像。同样,这在 vanilla JS 中正常工作,但在 JSX 中不正常。

我正在potentially invalid reference access to a class field via this in a nested functiondocument.querySelector("#file-input").addEventListener("change", this.previewImages);线,我认为这是导致问题的原因。

我做错了什么,我该如何解决?

import React, {Component} from 'react';

class Wall extends Component {
    constructor(props) {
        super(props);
        this.previewImages = this.previewImages.bind(this);
    }

    previewImages() {
        let preview = document.createElement("div");

        if (this.files) {
            [].forEach().call(this.files, readAndPreview());
        }

        function readAndPreview() {
            if (!/\.(jpe?g|png|gif)$/i.test(file.name)) {
                return alert(file.name + " is not an image");
            }

            let reader = new FileReader(); …
Run Code Online (Sandbox Code Playgroud)

javascript debugging reactjs

13
推荐指数
2
解决办法
1万
查看次数

标签 统计

debugging ×1

javascript ×1

reactjs ×1