Created
April 20, 2018 15:02
-
-
Save panchaow/c82740fe507e0f13f856e05996e55a41 to your computer and use it in GitHub Desktop.
File input in React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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