I need to implement click on the file input element in react based on the value selected. I below code is working fine on Linux - chrome but not working properly on windows chrome.
import React, { Component } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
class App extends Component {
constructor(props) {
super(props);
}
handleChange(e) {
if (e.target.value === "upload") {
this.fileExplorer.click();
}
}
openFileExplorer(e) {
this.fileExplorer.click();
}
render() {
return (
<div className="App">
<button onClick={this.openFileExplorer.bind(this)}>Open</button>
<select onChange={this.handleChange.bind(this)}>
<option key="none">None</option>
<option key="upload">upload</option>
</select>
<input
type="file"
accept=".json"
style={{ display: "none" }}
ref={input => (this.fileExplorer = input)}
/>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Working example on linux https://codesandbox.io/s/2wl95onqor?fontsize=14
Could you try this?
this.openFileExplorer = this.openFileExplorer.bind(this);
this line should add end of the constructor.
<button onClick={this.openFileExplorer}>Open</button>
<select onChange={this.handleChange}
You should change these lines like this. I hope it will work (i'm using macos) You can check this link; https://reactjs.org/docs/handling-events.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.