Files
react.dev/examples/uncontrolled-components/input-type-file.js
Fran Zekan 91ffdb0812 Upgrade to Node 12 LTS (#2831)
* Upgrade to node 12 LTS

* Run prettier
2020-03-14 13:24:39 +00:00

35 lines
735 B
JavaScript

class FileInput extends React.Component {
constructor(props) {
// highlight-range{3}
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.fileInput = React.createRef();
}
handleSubmit(event) {
// highlight-range{4}
event.preventDefault();
alert(
`Selected file - ${this.fileInput.current.files[0].name}`
);
}
render() {
// highlight-range{5}
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')
);