Skip to content

Instantly share code, notes, and snippets.

@panchaow
Created April 20, 2018 15:02
Show Gist options
  • Save panchaow/c82740fe507e0f13f856e05996e55a41 to your computer and use it in GitHub Desktop.
Save panchaow/c82740fe507e0f13f856e05996e55a41 to your computer and use it in GitHub Desktop.
File input in React
// https://reactjs.org/docs/uncontrolled-components.html#the-file-input-tag
class FileInput extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
event.preventDefault();
alert(
`Selected file - ${this.fileInput.files[0].name}`
);
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Upload file:
<input
type="file"
ref={input => {
this.fileInput = input;
}}
/>
</label>
<br />
<button type="submit">Submit</button>
</form>
);
}
}
ReactDOM.render(
<FileInput />,
document.getElementById('root')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment