Created
October 20, 2019 19:15
-
-
Save heypoom/6c7fcd39fc016dae32606abef56042d2 to your computer and use it in GitHub Desktop.
This file contains 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 * 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