# Tracking Upload progress

**URL:** <https://forum.codewithmosh.com/t/tracking-upload-progress/29883>\
**Category:** React Native\
**Created:** [November 11, 2024, 4:56pm UTC](https://forum.codewithmosh.com/t/tracking-upload-progress/29883 "2024-11-11T16:56:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arianna](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@Arianna](https://forum.codewithmosh.com/u/Arianna)\
**Post date:** [November 11, 2024, 4:56pm UTC](https://forum.codewithmosh.com/t/tracking-upload-progress/29883/1 "2024-11-11T16:56:55Z")

</div>

![immagine](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/7/78e8e944afad66b5db54070197a8408795b24dba.png)

Hello everybody,  
somebody experimented the same error? I am in the second part (backend) of the JavaScript course and in the Upload progress doesn’t work. I tried to fix the issue with Axios, but it doesn’t work either. Some suggestions?  
Thank you!

---

<div class="post-metadata">

**Author:** ![lucidlear](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/lucidlear/32/5477_2.png) [@lucidlear](https://forum.codewithmosh.com/u/lucidlear)\
**Post date:** [November 11, 2024, 9:14pm UTC](https://forum.codewithmosh.com/t/tracking-upload-progress/29883/2 "2024-11-11T21:14:08Z")

</div>

> [@Arianna](#):
>
> ome suggestions?

Hi, I think this is a common issue. Here is a couple of links to threads which might help:

[https://forum.codewithmosh.com/t/onuploadprogress-doesnt-work/22629/11](https://forum.codewithmosh.com/t/onuploadprogress-doesnt-work/22629/11)

[https://forum.codewithmosh.com/t/15-tracking-upload-progress/22286](https://forum.codewithmosh.com/t/15-tracking-upload-progress/22286)

If you really stuck then the below code works on [snack](https://snack.expo.dev/) (react-native in the browser) - just add all the dependancies at the bottom when prompted and run it on choosen platform:

App.js

```auto
import React, { useState } from "react";
import { Modal, View, StyleSheet, Button, Text } from "react-native";
import * as Progress from "react-native-progress";

const colors = { primary: "#3498db" };

function UploadScreen({ onDone, progress = 0, visible = false }) {
  return (
    <Modal visible={visible} transparent>
      <View style={styles.container}>
        {progress < 1 ? (
          <Progress.Bar
            color={colors.primary}
            progress={progress}
            width={200}
          />
        ) : (
          // Replacing LottieView with a Text message for Snack preview
          <Text style={styles.message}>Upload Complete!</Text>
        )}
      </View>
    </Modal>
  );
}

const styles = StyleSheet.create({
  container: {
    alignItems: "center",
    flex: 1,
    justifyContent: "center",
    backgroundColor: "rgba(0,0,0,0.7)"
  },
  message: {
    color: colors.primary,
    fontSize: 18,
    fontWeight: "bold"
  }
});

export default function App() {
  const [visible, setVisible] = useState(true);
  const [progress, setProgress] = useState(0);

  // Simulate upload progress
  React.useEffect(() => {
    if (progress < 1) {
      const interval = setInterval(() => {
        setProgress((prev) => Math.min(prev + 0.1, 1));
      }, 500);
      return () => clearInterval(interval);
    }
  }, [progress]);

  return (
    <View style={{ flex: 1, justifyContent: "center" }}>
      <Button title="Show Upload Screen" onPress={() => setVisible(true)} />
      <UploadScreen
        visible={visible}
        progress={progress}
        onDone={() => setVisible(false)}
      />
    </View>
  );
}

```

---

<div class="post-metadata">

**Author:** ![CrisRL](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@CrisRL](https://forum.codewithmosh.com/u/CrisRL)\
**Post date:** [November 11, 2024, 9:17pm UTC](https://forum.codewithmosh.com/t/tracking-upload-progress/29883/3 "2024-11-11T21:17:51Z")

</div>

Hi, yes, and I solved it.

Feel free to check my repo for the code:

> <https://github.com/CrisRamosLazaro/SparkJoy/blob/master/app/api/listings.js>

> <https://github.com/CrisRamosLazaro/SparkJoy/blob/master/app/screens/ListingEditScreen.jsx>

Basically not even XMLHttpRequest was working, so I went with an “extra manual” implementation 😃

---

<div class="post-metadata">

**Author:** ![Arianna](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@Arianna](https://forum.codewithmosh.com/u/Arianna)\
**Post date:** [November 12, 2024, 3:29pm UTC](https://forum.codewithmosh.com/t/tracking-upload-progress/29883/4 "2024-11-12T15:29:05Z")

</div>

Thank you so much !! it works !! the only problem is that when I push the button the bar appears before to charge the data. I ll let you know if I solve the thing! 🙂

---

<div class="post-metadata">

**Author:** ![Arianna](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@Arianna](https://forum.codewithmosh.com/u/Arianna)\
**Post date:** [November 12, 2024, 3:29pm UTC](https://forum.codewithmosh.com/t/tracking-upload-progress/29883/5 "2024-11-12T15:29:50Z")

</div>

Thank you for sharing the entire project !! Did you complete the all course? I am in the second part of the backend

---

<div class="post-metadata">

**Author:** ![CrisRL](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@CrisRL](https://forum.codewithmosh.com/u/CrisRL)\
**Post date:** [November 12, 2024, 5:10pm UTC](https://forum.codewithmosh.com/t/tracking-upload-progress/29883/6 "2024-11-12T17:10:15Z")

</div>

> [@Arianna](#):
>
> the bar appears before to charge the data

Sorry, I don’t understand what you mean 🙈  
What bar? Appears before what?  
By “charge the data” you mean “upload the data”?

I haven’t done the progress bar yet, but what I’ve noticed is that if I try to add a listing more than once, to test, the progress bar will start at 100% before it goes back to 0%.  
Maybe that’s what you mean.  
setting uploadProgress back to zero when you hide the modal shall do the trick:  
const formDataWithoutImg = { title, price, categoryId: category.id, description, location }

```
    try {
        const response = await listingsApi.createListing(data, formDataWithoutImg, setUploadProgress)
        setIsModalVisible(false)
        setUploadProgress(0) //---> try this

        alert('Success')
    } catch (error) {
        console.error('Error creating listing:', error)
        alert('Error saving your listing')
    }
}

```

Ref your question in the 2nd comment, no, I haven’t finished, I spent some hours trying to find a solution for getting the progress data.

Note: in the end the problem with the XMLHttpRequest solution was an error in the response, saying that location must be of type object. I tried parsing it, but no luck. The problem persists with my solution, I only didn’t notice because as it is, the `createListing` function is not returning anything (if you console.log this —\>  
`const response = await listingsApi.createListing(data, formDataWithoutImg)` you’ll get `undefined`)

Now that I have it, I can move on 🕶

---

<div class="post-metadata">

**Author:** ![Arianna](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@Arianna](https://forum.codewithmosh.com/u/Arianna)\
**Post date:** [November 20, 2024, 3:23pm UTC](https://forum.codewithmosh.com/t/tracking-upload-progress/29883/7 "2024-11-20T15:23:14Z")

</div>

![immagine](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/9/9428cd3394e2a0d45adb0372661732bf5328d153.jpeg)  
look at this. Is what I wanted to say 😃 The problem is that is not charging, but since we are in a simulation I understand that we can move on right?
