简体   繁体   English

在this.forceUpdate()或this.setState(this.state)之后,React不重新呈现DOM

[英]React not re-rendering DOM after this.forceUpdate() or this.setState(this.state)

I'm having an issue re-rendering the DOM after something has been spliced from an array in my state variable. 从状态变量中的数组拼接出某些内容后,重新渲染DOM时出现问题。 Here is the code for this component: 这是此组件的代码:

class AddMusicResource extends Component {


removeFile(file, event) {
    var filesPreview = this.state.filesPreview
    var filesToBeSent = this.state.filesToBeSent
    var i = filesToBeSent.indexOf(file)
    console.log(i)
    if(i !== -1) {
        filesToBeSent.splice(i, 1)
        filesPreview.splice(i, 1)
    }
    this.setState({filesToBeSent, filesPreview});   
    this.setState(this.state)

}

onDrop(acceptedFiles, rejectedFiles) {
  var filesToBeSent=this.state.filesToBeSent;
  for(var file in acceptedFiles) {
    console.log('Accepted files: ', acceptedFiles[file].name);
    filesToBeSent.push(acceptedFiles[file]);
  }

    var filesPreview=[];
    for(var i in filesToBeSent){
      filesPreview.push(
        <div key={i}>
            {filesToBeSent[i].name}
            <IconButton
                  iconClassName="material-icons"
                  onClick={this.removeFile.bind(this, filesToBeSent[i])}
                >
                  close
            </IconButton>
        </div>
      )
    }
    this.setState({filesToBeSent, filesPreview});
}

render() {
    return (
    <Card shadow={0} style={{ margin: '10px'}}>
        <CardTitle>Add Music Resources</CardTitle>
        <CardText >
           <form id="upload-form">              
                <Dropzone 
                    onDrop={(files) => this.onDrop(files)}
                    multiple={true}
                >
                    <div>Try dropping some files here, or click to select files to upload.</div>
                </Dropzone>
                <div>
                     Files to be uploaded are:
                     {this.state.filesPreview}
                </div>

                <input className={this.state.buttonClasses} type="submit" value="Submit" />
            </form>
        </CardText>
    </Card>
    );
}
}

export default AddMusicResource;

So basically I'm taking a file input and then pushing the name of the file to a filePreview array that is being rendered on the page, but when a user clicks the close x to the right of the name I expect that the name of the file would be removed from the DOM. 所以基本上我要输入文件,然后将文件名推送到页面上呈现的filePreview数组中,但是当用户单击名称右边的close x时,我希望文件将从DOM中删除。 The removeFile() is working as intended and is indeed removing the correct element from the page, but the only time that the DOM would update to see that file as removed is if I added another file using the Dropzone. removeFile()可以按预期工作,并且确实正在从页面中删除正确的元素,但是DOM更新以查看该文件是否被删除的唯一时间是我是否使用Dropzone添加了另一个文件。

I was wondering if anyone could help me figure out why the DOM is not rerendering at the end of my removeFile() function? 我想知道是否有人可以帮助我找出为什么在我的removeFile()函数末尾不重新渲染DOM的原因?

I have tried both this.setState(this.state) and this.forceUpdate() but neither have been successful. 我已经尝试过this.setState(this.state)this.forceUpdate()但都没有成功。

You are mutating the state and React has a hard time to find what actually changed. 您正在改变状态,React很难找到实际更改的内容。 Try this instead: 尝试以下方法:

removeFile(file, event) {
    var i = this.state.filesToBeSent.indexOf(file);

    if (i < 0)
        return;

    this.setState((prevState) => {
        return {
            filesToBeSent: prevState.filesToBeSent.filter((element, index) => index !== i),
            filesPreview: prevState.filesPreview.filter((element, index) => index !== i)
        };
    });
}

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

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