javascript - Reactjs:将 <input type ="file"> 转换为自定义按钮图标的问题

标签 javascript reactjs

我是 React 的新手,正在尝试构建一个应用程序,我想在其中按下一个自定义按钮,该按钮将打开一个文件对话框并在选择它时上传一个文件。这是我的代码:

class ComposeButtons extends Component{

    constructor(props) {
        super(props);
        this.state={
            selectedFile: null
        };
        this.myInput = React.createRef();
    }

    fileSelectedHandler = (event) => {
        console.log(event.target.files[0]);
        this.setState({
            selectedFile: event.target.files[0]
        })
    };
    triggerClick = () => {
        this.myInput.current.click()
    };

    fileUploadHandler = () => {
      /* file upload triggered */
      console.log('file upload triggered');
    };

    render() {
        return(
            <div className={"composeForm-area"}>
                <div>
                    <Input
                        style={{display:'none'}}
                        type={"file"}
                        onChange={this.fileSelectedHandler}
                        ref={this.myInput}
                    />
                    <Button onClick={this.triggerClick}
                            onChange={this.fileUploadHandler} 
                            style={{ backgroundColor: '#DDDCDC'}}>
                        <Icon style={{ fontSize: '20px'}} type="camera" />
                    </Button>
                </div>
            </div>
        )
    }
}
export default ComposeButtons;

我当前的输出:

enter image description here

我只得到一个像上面这样的可点击图标,但是,点击它会抛出:
Uncaught TypeError: _this.myInput.current.click is not a function
    at eval (ComposeButtons.js:88)
    at Button._this.handleClick (button.js:143)
    at HTMLUnknownElement.callCallback (react-dom.development.js:14

我想要的:

我只是想在单击此相机按钮时打开一个文件对话框来选择文件,然后在文件对话框中选择并按确定后,它应该关闭并触发 fileUploadHandler在控制台上打印消息的功能。就这样!

我试过的:

除了上面的代码,我尝试用以下代码替换渲染方法中的 div 内的代码:
        <div>
            <Input
                id="myInput"
                style={{display:'none'}}
                type={"file"}
                onChange={this.fileSelectedHandler}
                ref={(ref) => this.myInput = ref}
            />
            <Button onClick={(e) => this.myInput.click() }
                    style={{ backgroundColor: '#DDDCDC'}}>
                <Icon style={{ fontSize: '20px'}} type="camera" />
            </Button>
        </div>

我也遵循了 stackoverflow 上的几乎所有答案,但似乎没有什么对我有用。如果有人能指出我正确的方向,那将非常有帮助。

这是我在 React 中的第一个爱好项目。

最佳答案

据我得到你的问题。我们所能做的就是添加一个label标签引用输入类型file使用 for标签标签中的属性。通过这样做,我们不需要使用 ref
有关此 link 的信息.

现在需要做的就是为 label 编写适当的 css。标签

<div>
  <label htmlFor="myInput"><Icon style={{ fontSize: '20px'}} type="camera" /></label>
  <input
    id="myInput"
    style={{display:'none'}}
    type={"file"}
    onChange={this.fileSelectedHandler}
  />
</div>

之后,触发文件上传。我们可以调用fileUploadHandlerfileSelectedHandler 之后叫做。
fileSelectedHandler = (event) => {
  console.log(event.target.files[0]);
  this.setState({
      selectedFile: event.target.files[0]
  }, () => this.fileUploadHandler());
};

关于javascript - Reactjs:将 &lt;input type ="file"> 转换为自定义按钮图标的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57571752/

相关文章:

javascript - Google Pay 和网络支付请求 API :

javascript - Chrome 扩展程序可重定向到带参数的 URL

javascript - 用谷歌的sw-precache搭建的service worker真的能做到networkFirst吗?

javascript - 将 UI Fabric PeoplePicker 与 React 结合使用需要什么特别的东西吗?

javascript - 垃圾收集和 jQuery?

javascript - 如何在 MathJax 中使用波斯变量?

reactjs - React 项目上的运行错误

javascript - 为什么我会收到此错误 - 'You are calling withTheme(Component) with an undefined component.' -React JS

reactjs - 如何将文本输入返回到 React 组件

javascript - 尝试增量转换为 Typescript 时,非 Typescript 导入失败