how get get file name in file chooser in react?
You can get selected file name without using ref
function handleChange(event) {
console.log(`Selected file - ${event.target.files[0].name}`);
}
<input type="file" onChange={handleChange} />
Good documentation and example taken from here, explaining what you are trying to do. https://reactjs.org/docs/uncontrolled-components.html#the-file-input-tag
Codepen: https://codepen.io/anon/pen/LaXXJj
React.JS contains a specific File API to use.
The following example shows how to create a ref to the DOM node to access file(s) in a submit handler:
HTML
<input type="file" />
React.JS
class FileInput extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.fileInput = React.createRef();
}
handleSubmit(event) {
event.preventDefault();
alert(
`Selected file - ${
this.fileInput.current.files[0].name
}`
);
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Upload file:
<input type="file" ref={this.fileInput} />
</label>
<br />
<button type="submit">Submit</button>
</form>
);
}
}
ReactDOM.render(
<FileInput />,
document.getElementById('root')
);
Alert Filename
alert(`Selected file - ${this.fileInput.current.files[0].name}`);
Cited: React.JS Documentation | Examples