Topic: Selecting a corrected document leaves the previous upload error visible
Hannah Berg
free
asked 1 week ago
Our file picker rejects oversized documents and displays a validation message below the upload area. Choosing a smaller document afterward updates the filename, but the original error remains visible until the entire form is reset.
The size check runs in our wrapper before a request is created. We keep the selected File and the error in separate state values. Both picker selection and dropping a document use the same validation function.
Mateusz Lazaru
staff
answered 1 week ago
Set the error for every validation result, including a successful selection. A branch that only calls setError when a file is too large will retain the earlier error when the next file passes the check.
Apply the same rule when the selection is cleared. If selecting the same rejected file again does not trigger your native input handler, clear that input's value after reading its File objects; keep the actual queue in React state.
function validateFile(file) {
const error = file && file.size > maxBytes ? 'Document is too large.' : '';
setError(error);
setSelectedFile(error ? null : file);
}
https://react.dev/reference/react-dom/components/input
Hannah Berg free commented 6 days ago
That was the missing branch. We now clear the error when a valid file is selected and when the queue is emptied. I checked picking and dropping files, and the corrected document no longer keeps the previous message. Thanks for the clear explanation.
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: Laptop
- Browser: Edge
- OS: Windows 11
- Provided sample code: No
- Provided link: No