Skip to content

Instantly share code, notes, and snippets.

@somidad
Created June 7, 2023 14:20
Show Gist options
  • Save somidad/603eeb9829e591f7e57bf88931100947 to your computer and use it in GitHub Desktop.
Save somidad/603eeb9829e591f7e57bf88931100947 to your computer and use it in GitHub Desktop.
Read a file on React
function Component() {
const onChange = (e) => {
const file = e.target.files?.[0];
if (!file) {
return;
}
const reader = new FileReader();
reader.addEventListener('load', () => {
console.log(reader.result);
});
reader.readAsText(file);
}
return (
<div>
<input type='file' onChange={onChange} />
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment