Skip to content

Instantly share code, notes, and snippets.

@heypoom
Created October 20, 2019 19:15
Show Gist options
  • Save heypoom/6c7fcd39fc016dae32606abef56042d2 to your computer and use it in GitHub Desktop.
Save heypoom/6c7fcd39fc016dae32606abef56042d2 to your computer and use it in GitHub Desktop.
import * as React from 'react'
import {useState} from 'react'
import ReactDOM from 'react-dom'
function FileReader() {
const [text, setText] = useState('')
function onChange(e: React.ChangeEvent<HTMLInputElement>) {
const {files} = e.target
const [file] = files
console.log('File:', file)
const reader = new window.FileReader()
reader.onload = e => setText(String(e.target.result))
reader.readAsText(file)
}
return (
<div>
<div>
<input type="file" onChange={onChange} />
</div>
<code>{text}</code>
</div>
)
}
ReactDOM.render(<FileReader />, document.querySelector('#app'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment