Last active
October 19, 2017 08:06
-
-
Save macabreb0b/c3508118f6009bdc9f1e162753fab0ae to your computer and use it in GitHub Desktop.
handle photo upload with 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
import React, { Component } from 'react'; | |
class PhotoInput extends Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
fileData: '', | |
} | |
this.handleInputChange = this.handleInputChange.bind(this); | |
} | |
handleInputChange(event) { | |
const input = event.target | |
const file = input.files[0] | |
const reader = new FileReader(); | |
reader.onload = (event) => { | |
this.setState({ | |
fileData: reader.result | |
}) | |
} | |
reader.readAsDataURL(file) | |
} | |
render() { | |
return ( | |
<div> | |
<label> | |
I am a photo input | |
<input | |
onChange={ this.handleInputChange } | |
type='file' /> | |
</label> | |
</div> | |
) | |
} | |
} | |
export default PhotoInput; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment