I have a component with an <input type='file'/> to select and upload images to my Storage bucket.
I'm using FileReader().readAsText(file); which is asynchronous and I'm setting to listeners to the onload and onerror event.
From: https://developer.mozilla.org/en-US/docs/Web/API/FileReader
Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.
This runs everytime a user selects a file. I'm calculation an md5 hash to use as the fileName in the storage.
if (newFile !== undefined && newFile !== null) {
md5Hash = await new Promise((resolve,reject) => {
const reader = new FileReader();
reader.onload = (event) => {
console.log('md5 calculated!');
const binary = event.target.result;
resolve(md5(binary));
};
reader.onerror = (event) => {
reject(event.target.result);
};
reader.readAsText(newFile);
});
}
Do I need to bother to remove those listeners after I'm done with them? Is this a good practice or there's no point in doing that?
That code may run 5 or 6 times for different images that I'm uploading in the same page.
NOTE: It's a page to add blogPosts and the images for the posts.