使用 ref 触发反应 dropzone 不起作用

2024-05-18

我正在实现这个库:https://github.com/felixrieseberg/React-Dropzone-Component https://github.com/felixrieseberg/React-Dropzone-Component

要以编程方式触发另一个组件或元素,我可以使用ref但我得到了一个错误photoUploadDropAreaElement不是使用以下代码的函数。

triggerUploadDialog(){
    this.photoUploadDropAreaElement.click(); // doesn't work?

    console.log(this.photoUploadDropAreaElement);
}

render() {
return(
        <div onClick={this.triggerUploadDialog.bind(this)}>
            <DropzoneComponent ref={dropArea => this.photoUploadDropAreaElement = dropArea} />
        </div>
);

The result of DropzoneComponent look like this enter image description here

这是怎么回事?我只想触发单击打开文件对话框,供用户选择要上传的文件。


我在用着import * as Dropzone from 'react-dropzone'; via npm install react-dropzone --save-dev. Go here https://react-dropzone.js.org/了解详情。

默认情况下,此 dropzone 包允许您单击 UI 的 dropzone 打开文件对话框,供用户选择要上传的文件。

这是我使用的代码,其中包括“选择文件”按钮和“删除”按钮。 *笔记:multiple={false}禁用用户选择多个文件的能力。您只需将其更改为 true 即可启用多个文件选择。

import * as React from 'react';
import * as Dropzone from 'react-dropzone';

export interface FileUploadState { file: Array<File> }

export class FileUpload extends React.Component<{}, FileUploadState> {
    constructor(props: any) {
        super(props);
        this.state = {
            file: []
        }
    }

    onDrop(droppedFile: any) {
        if (droppedFile && droppedFile.preview) {
            window.URL.revokeObjectURL(droppedFile.preview);
        }
        this.setState({
            file: droppedFile
        });
        console.log(droppedFile);
    }

    onDelete() {
        this.setState({
            file: []
        });
    }

    render() {
        let dropzoneRef: any;
        return (
            <div>
                <div>
                    <Dropzone ref={(node) => { dropzoneRef = node; }} onDrop={this.onDrop.bind(this)} multiple={false}>
                        <div className="text-center">Drag and drop your file here, or click here to select file to upload.</div>
                    </Dropzone>
                    <button type="button" className="button primary" onClick={(e) => {
                        e.preventDefault(); // --> without this onClick fires 3 times
                        dropzoneRef.open();
                    }}>
                        Choose File(s)
                    </button>
                    <button type="button" className="button alert" onClick={this.onDelete.bind(this)}>
                        Delete
                    </button>
                </div>
                <hr />
                <div>
                    <h2>File(s) to be uploaded: {this.state.file.length} </h2>
                    <ul>
                        {
                            this.state.file.map(f => <li><code>{f.name}</code></li>)
                        }
                    </ul>
                </div>
            </div>

        )
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 ref 触发反应 dropzone 不起作用 的相关文章

随机推荐