PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 14, 2022

[FIXED] How to change Axios onDownloadProgress event.isTrusted = false to true in react-native

 October 14, 2022     api, axios, javascript, react-native     No comments   

Issue

I'm using Axios "^0.27.2" for fetching data in react-native "0.62.2" and I'm trying to create a percentage loader using content-length which I'm getting from server in response header.

API GET request code

const baseResponse = axios.get("https://reqres.in/api/users", {
            onDownloadProgress: (progressEvent) => {
                console.log(progressEvent) // progress is set every 10 milliseconds
            },
            headers: {
                "Content-Type": "application/json",
                "Authorization": `Bearer ${Config.UserSession.USER_DATA.token}`
            }

        }).then(res => { return res.data;})
        .catch(error => {
            console.log(error)
            return null
        })

Above code is working fine I'm able to fetch data properly. but when I'm trying using onDownloadProgress progressEvent.loaded is coming -1 and also progressEvent.isTrusted=false

console log of progressEvent

 {"isTrusted": false, "lengthComputable": false, "loaded": -1, "total": -1}

lengthComputable=false because content-length is not yet configure. I don't know where I'm doing wrong because I used the same code in simple Javascript it was working fine.


Solution

For Some Reason onDownloadProgress doesn't work normally in react-native If anyone facing same problem solution is here - solution

use

 onDownloadProgress: (event) => {
                console.log(event.target._response.length) // loaded
                console.log(event.target.responseHeaders['Content-Length']) // total
            },


Answered By - Holasmabre
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing