I'm uploading multiples images and I wanted to display the correct percentage "50%". However I'm calling the API one by one. Meaning I upload the image one by one.
The problem is the percentage keeps getting back and forth like 0 - 50%, then going back again to 0 and so on... The reason for this is because I'm dispatching the setProgress multiples times.
What will be the correct calculation to achieve the correct percentage?
Reducer
case SET_UPLOAD_PROGRESS: return { ...state, uploadProgress: action.payload, } Actions
export const uploadImages = ({ images }) => (dispatch) => { images.forEach(async (image) => { const formData = new FormData(); formData.append(image.name, image.imageFile); try { dispatch({ type: UPLOAD_IMAGES_REQUEST }); const response = await axios.post("http://test.com", formData, { onUploadProgress(progressEvent) { const { loaded, total } = progressEvent; const percentage = Math.floor((loaded / total) * 100); dispatch(setUploadProgress(percentage)); <- GETTING PERCENTAGE. IT IS WORKING ALREADY. EXAMPLE OUTPUT: 50 }, }); dispatch({ type: UPLOAD_IMAGES_SUCCESS, payload: [...(images || [])], }); } catch (error) { dispatch({ type: UPLOAD_IMAGES_FAILURE }); } }); }; Component
const uploadProgress = useSelector(state => state.images.uploadProgress); <LinearProgress variant="determinate" value={uploadProgress} />