I want to create a simple files uploader, I have listOfFiles coming from dropzone drag and drop already.
A simple loop over the files
{listOfFiles.map((file, i) => (
<SingleFile
key={i}
index={i}
file={file}
handleDelete={handleDelete}
/>
))}
Inside of SingleFile I do axios API calls to upload the file when the component mount using useEffect simplified as this:
useEffect(() => {
if (!props.file) return;
// ...
// Axios call and bunch of states changes.
// ...
}, [props.file]);
Upload process is just fine & When I do drag another files it works, New files added to listOfFiles and then start uploading.
Now when I click on the delete button inside SingleFile, handleDelete on parent component fires to delete the files with the specific index.
<button onClick={props.handleDelete(index)}> Delete </button>
on handleDelete
setListOfFiles((listOfFiles) => listOfFiles.filter((f, i) => i !== index));
Now the problem is when I delete a SingleFile the listOfFiles array is updated and each array element which is a SingleFile component is re-rendered and the upload axios call is called again... etc.
Is there any way to not re-render an Instance of component when another instance changes? to make it depends only on it's state/props when it comes to re-render? and to control it's mount/unmount by unique id or index in the parent array.