Topic: Removing an attachment leaves its upload request running
monoStudio42
free
asked 1 week ago
We upload each selected document immediately and render a remove button beside its progress indicator. Removing a document hides it from the queue, but its request continues and the finished attachment sometimes appears again when the response arrives.
The queue is owned by our React wrapper and each entry already has a stable ID. What is a sensible way to cancel the matching request without canceling the other files?
Mateusz Lazaru
staff
answered 1 week ago
Keep an AbortController for each queue ID and pass its signal to the fetch request. When removing an entry, abort that controller before updating the queue. Treat AbortError as a canceled operation rather than a failed upload.
The response handler should update an entry only if it is still present. That also protects against a response that completed just before cancellation. Remove completed controllers from the map and abort any remaining requests when the wrapper unmounts.
const controllers = useRef(new Map());
function removeFile(id) {
controllers.current.get(id)?.abort();
controllers.current.delete(id);
setQueue(current => current.filter(item => item.id !== id));
}
https://react.dev/reference/react-dom/components/input
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- ForumUser: Free
- Premium support: No
- Technology: MDB React
- MDB Version: MDB5 9.0.0
- Device: Desktop
- Browser: Firefox
- OS: Linux
- Provided sample code: No
- Provided link: No