简体   繁体   English

如何在 reactjs 中使用“document.getElementById('file').click()”

[英]How to use“ document.getElementById('file').click()” in reactjs

I searched for a below javascript code and I want to use it in Reactjs:我搜索了下面的 javascript 代码,我想在 Reactjs 中使用它:

    <input type="button" id="loadFileXml" value="loadXml" onclick="document.getElementById('file').click();" />
    <input type="file" style="display:none;" id="file" name="file"/>

In order to use it in Reactjs, I changed it to:为了在 Reactjs 中使用它,我将其更改为:

    <input type="button" id="loadFileXml" value="loadXml" onClick={document.getElementById('file').click()} />
    <input type="file" style="display:none;" id="file" name="file"/>

But I got error message Cannot read property 'click' of null .但我收到错误消息Cannot read property 'click' of null Could somebody help me figure out how to write the write code for that.有人可以帮我弄清楚如何为此编写编写代码。

Thanks so much.非常感谢。

You can't use react js in this way.你不能以这种方式使用 react js。 First, you can use a state for the display property.首先,您可以使用 state 作为显示属性。 Suppose there is a class component.假设有一个 class 组件。 I wrote it with my hand, it could be a typo.我是手写的,可能是笔误。

import React, {Component} from 'react';

class Example extends Component {
 state = {
  isVisible : true ; 
} 

  changeVisible = () => {

    this.setState({

       isVisible : !this.state.isVisible // this working like a toggle button 
    })

   }

render (){
    return(
   <div>
        <button onClick = {this.changeVisible}> Change Visible </button>
        {
           //This section may be the item that you want to be visible or not
           isVisible ? <h1>I am Visible </h1> : null

        }
   </div>

)
}


} 
  export default Example;

Disclaimer: I personally don't think you should be hiding the file input control and delegating the click to another element.免责声明:我个人认为您不应该隐藏文件输入控件并将点击委托给另一个元素。

That said, the way to do this with React is to get a reference to the file input element using the ref property and React.createRef method:也就是说,使用 React 执行此操作的方法是使用ref属性和React.createRef方法获取对文件输入元素的引用:

Refs are created using React.createRef() and attached to React elements via the ref attribute. Refs 使用React.createRef()创建并通过ref属性附加到 React 元素。 Refs are commonly assigned to an instance property when a component is constructed so they can be referenced throughout the component. Refs 通常在构建组件时分配给实例属性,以便可以在整个组件中引用它们。

Demo:演示:

 class MyComponent extends React.Component { constructor(props) { super(props); this.fileRef = React.createRef(); } handleClick = () => { const fileElem = this.fileRef.current; if (fileElem) { fileElem.click(); } } render() { return ( <div> <button onClick={this.handleClick}>button</button> <input ref={this.fileRef} type="file" style={{display: 'none'}} name="file" /> </div> ); } } ReactDOM.render( <MyComponent />, document.getElementById('app') );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="app"></div>

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

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