Skip to content

Instantly share code, notes, and snippets.

@Olegas
Last active April 16, 2019 15:28
Show Gist options
  • Select an option

  • Save Olegas/30006f696412908ae68ba88c37254de1 to your computer and use it in GitHub Desktop.

Select an option

Save Olegas/30006f696412908ae68ba88c37254de1 to your computer and use it in GitHub Desktop.
const [images, setImages] = useState([]);
function loadFileAsync(file) {
const reader = new FileReader();
return new Promise((resolve, reject) => {
reader.onloadend = (e) => {
resolve(e.target.result);
};
// TODO handle error
reader.readAsDataURL(file);
});
}
function handleChanges(e) {
const {files} = e;
if (files.length) {
Promise.all(files.map((file) => loadFileAsync(file))).then((images) => setImages(images));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment