Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could be causing this slow fetch in react native?

Tags:

react-native

In the following code, the first console.log message prints pretty much instantly. Then everything just hangs (I'm initially assumed it was waiting for the body of the response to be returned). The Body of the response is only about 26K, the time waiting seems to be indefinite UNLESS, I shake the phone and interact with the debug menu. As soon as I interact with the debug menu, the promise resolves and everything moves along as expected. My interactions with the debug menu can be simple, like hide inspector, show inspector, just takes something to kick the promise resolution into gear and everything is fine.

fetch(SEARCH_URL, requestBody)
    .then((response) => {console.log(response); return response.json();})
    .then((responseData) => {
        debugger
        ...

Note: Disconnecting from the debugger and running the code does not exhibit the slowness (and not being connected to the debugger ignores the debugger statements)

And yes, I have rebooted the computer.

Might have found something in https://github.com/facebook/react-native/issues/6679

like image 386
boatcoder Avatar asked Mar 28 '16 12:03

boatcoder


People also ask

Why is my React Native app so slow?

Memory leakage, a React Native performance issue, may occur due to unnecessary processes that run in the background in an Android app. Try using scrolling lists like SectionList, FlatList, or VirtualList, instead of ListView.

Does fetch work with React Native?

React Native provides the Fetch API for your networking needs. Fetch will seem familiar if you have used XMLHttpRequest or other networking APIs before.

What is data fetching in React?

React uses the thrown value to detect if the component is ready to be rendered. Once data is fetched, you just need to throw the data, and the component will be rendered. This very simple and quick method allows you to tell React that you want it to handle data fetching status instead of handling it yourself.


2 Answers

As you've found out yourself, this is a known bug that should be fixed in react-native v0.31

like image 156
damusnet Avatar answered Sep 22 '22 22:09

damusnet


What worked for me is moving the fetch calls inside the constructor of a react component. Otherwise they never resolve. Hope this helps

like image 35
saber Avatar answered Sep 22 '22 22:09

saber