简体   繁体   English

onChange() 不是函数。 手动触发 onChange()

[英]onChange() is not a function. Trigger onChange() manually

I've got a file selector, when clicking the button, the file selector shows up:我有一个文件选择器,当点击按钮时,文件选择器出现:

<input id='example' type="file" accept="image/*" onChange={this.onImageChange} />

Now I want to use another button to do that, trigger the input above :现在我想使用另一个按钮来做到这一点,触发上面的输入:

<button onClick={() => {
    var element = document.getElementById('example');

    console.log(element) // This shows <input id="example" type="file" accept="image/*">

    // element.onChange() // Error: onChange() is not a function

    // Above doesn't work, so I try to trigger the onChange event manually,
    // but below doesn't do anything
    var event = new Event('change');
    element.dispatchEvent(event);
}}></button>

If that is react you should not access the Dom for those kind of stuff.如果这是反应,您不应该访问 Dom 以获取此类内容。 They have one Api to give you what you need.他们有一个 Api 可以为您提供所需的东西。

You want one uncontroller component.您需要一个非控制器组件。 In order to do that you can use one reference.为此,您可以使用一个参考。

I havent tried but i think you can do this.我还没有尝试过,但我认为你可以做到这一点。

Create the ref on the constructor在构造函数上创建 ref

this.inputRef = React.createRef();

Then assign the input ref prop to this.inputRef然后将 input ref prop 分配给 this.inputRef

<input ref={this.inputRef} id='example' type="file" accept="image/*" onChange={this.onImageChange} />

And lately dispatch the click.最近发送点击。

this.inputRef.current.click();

Hope it works希望它有效

element.onChange is not a method because your onChange function is stored in your react Component class and delegated via React's synthetic event emitter. element.onChange不是方法,因为您的 onChange 函数存储在您的 react Component 类中,并通过 React 的合成事件发射器进行委托。 It is a property of React.createElement('input') , but is not a property of an actual DOM element.它是React.createElement('input')的属性,但不是实际 DOM 元素的属性。

To call your React element's onChange method, you can call it directly, like this.onChange(event) .要调用 React 元素的 onChange 方法,您可以直接调用它,例如this.onChange(event)

The problem with that methodology is your event will not have a target attached to it.该方法的问题在于您的活动没有附加目标。

The change method is meant to provide an interface for controlling the value of inputs, so if you want to change the file attached to the input, you should simply change its value in state. change 方法旨在提供一个用于控制输入值的接口,因此如果您想更改附加到输入的文件,您只需更改其状态值即可。

this.setState({file: this.alternateFile}) 

The onChange function is a callback AFTER a file has been selected. onChange 函数是选择文件后的回调。

If what you want is to show the file selector with another button, you can take a look into this answer如果您想要使用另一个按钮显示文件选择器,您可以查看此答案

Bear in mind, because of security reasons you need to trigger the selector on the onClick function, user input IS NECESSARY.请记住,出于安全原因,您需要在 onClick 函数上触发选择器,因此需要用户输入。

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

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