在 vanilla JS 中,我的代码可以正常工作。对于这种情况,我想组件化我的Wall类,该类应该在用户上传的浏览器中显示图像。同样,这在 vanilla JS 中正常工作,但在 JSX 中不正常。
我正在potentially invalid reference access to a class field via this in a nested function上document.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)