# React Native Part 2 (Networking: Fetching Data)

**URL:** <https://forum.codewithmosh.com/t/react-native-part-2-networking-fetching-data/1250>\
**Category:** React Native\
**Created:** [December 13, 2020, 6:01am UTC](https://forum.codewithmosh.com/t/react-native-part-2-networking-fetching-data/1250 "2020-12-13T06:01:33Z")\
**Posts on this page:** 10\
**Page:** 3

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [March 25, 2021, 10:51pm UTC](https://forum.codewithmosh.com/t/react-native-part-2-networking-fetching-data/1250/41 "2021-03-25T22:51:21Z")

</div>

I think it’s because the codeblock in brackets doesn’t return anything. In the second example it’s clear that the lambda should return the result of `apiClient.get(endpoint)`. If you supply a codeblock as the lambda body this block can contain several statements and you need to explicitly `return` a value if you want the arrow function to return something. So I believe `() => { return apiClient.get(endpoint);}` should work.

---

<div class="post-metadata">

**Author:** ![sollazzo08](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@sollazzo08](https://forum.codewithmosh.com/u/sollazzo08)\
**Post date:** [March 25, 2021, 10:53pm UTC](https://forum.codewithmosh.com/t/react-native-part-2-networking-fetching-data/1250/42 "2021-03-25T22:53:53Z")

</div>

Hey Sam,

Yeah that works as well and makes perfect sense haha! Just tested it.

---

<div class="post-metadata">

**Author:** ![Jonasliu90](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@Jonasliu90](https://forum.codewithmosh.com/u/Jonasliu90)\
**Post date:** [March 10, 2022, 2:35am UTC](https://forum.codewithmosh.com/t/react-native-part-2-networking-fetching-data/1250/43 "2022-03-10T02:35:51Z")

</div>

Hi everyone, I met the same issue in this section and I found my way to solve the issue. I can fetch the listings data and get the product images.

1. change the address of assetBaseUrl in the Backend → config → development.json to your localhost address.  

2. In the component client.js, use your localhost address.

---

<div class="post-metadata">

**Author:** ![lorenco](https://avatars.discourse-cdn.com/v4/letter/l/a587f6/32.png) [@lorenco](https://forum.codewithmosh.com/u/lorenco)\
**Post date:** [March 24, 2022, 7:09pm UTC](https://forum.codewithmosh.com/t/react-native-part-2-networking-fetching-data/1250/44 "2022-03-24T19:09:13Z")

</div>

hey everyone so I was getting the Network Error before, and couldn’t figure out why it wasn’t working. I was able to use localhost and 127.0.0.1 (just learned they are the same thing) but as Mosh said, using localhost won’t work for android devices.

I then found a post in the React Native Developers discord channel where someone said that you can’t fetch from localhost because localhost is your own phone (if you try using localhost and running the app on your own physical iOS device, you’ll see that you still get the network error even though it works in the simulator). The same person who posted the above mentioned message then said that you need to use your computer’s LAN IP address. I had been using the IP address i found when I googled “what is my IP address” since I didn’t know there were different types/purposes.

After following the instructions on this site ([get your LAN IP address in MacOSX](https://osxdaily.com/2010/08/08/lan-ip-address-mac/)), and I have fixed the issue and can attest that using my LAN IP address allows my iOS simulator, iOS physical device, AND android simulator to successfully connect to my API.

As a reminder, you’ll have to use the LAN IP address in the baseURL in client.js in the client AND assetsBaseUrl in development.json in the API.

I hope this helps!

---

<div class="post-metadata">

**Author:** ![lorenco](https://avatars.discourse-cdn.com/v4/letter/l/a587f6/32.png) [@lorenco](https://forum.codewithmosh.com/u/lorenco)\
**Post date:** [March 24, 2022, 7:16pm UTC](https://forum.codewithmosh.com/t/react-native-part-2-networking-fetching-data/1250/45 "2022-03-24T19:16:19Z")

</div>

hey everyone so I was getting the Network Error before, and couldn’t figure out why it wasn’t working. I was able to use localhost and 127.0.0.1 (just learned they are the same thing) but as Mosh said, using localhost won’t work for android devices.

I then found a post in the React Native Developers discord channel where someone said that you can’t fetch from localhost because localhost is your own phone (if you try using localhost and running the app on your own physical iOS device, you’ll see that you still get the network error even though it works in the simulator). The same person who posted the above mentioned message then said that you need to use your computer’s LAN IP address. I had been using the IP address i found when I googled “what is my IP address” since I didn’t know there were different types/purposes.

After following the instructions on OS X daily ([get your LAN IP address in MacOSX](https://osxdaily.com/2010/08/08/lan-ip-address-mac/)), I was able to fix the issue and can attest that using my LAN IP address allows my iOS simulator, iOS physical device, AND android simulator to successfully connect to my API.

As a reminder, you’ll have to use the LAN IP address in the baseURL in client.js in the client AND assetsBaseUrl in development.json in the API.

I hope this helps!

---

<div class="post-metadata">

**Author:** ![brettlee](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@brettlee](https://forum.codewithmosh.com/u/brettlee)\
**Post date:** [December 30, 2022, 6:00pm UTC](https://forum.codewithmosh.com/t/react-native-part-2-networking-fetching-data/1250/47 "2022-12-30T18:00:46Z")

</div>

Good morning, gentlemen. I’m experiencing the identical issue as mentioned above, and I’ve tried every potential option with no success. Why is this network fault a problem because I performed the same thing Mosh did[?](https://alightmotion.net/)

---

<div class="post-metadata">

**Author:** ![delboy1978uk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/delboy1978uk/32/4513_2.png) [@delboy1978uk](https://forum.codewithmosh.com/u/delboy1978uk)\
**Post date:** [January 5, 2023, 11:08am UTC](https://forum.codewithmosh.com/t/react-native-part-2-networking-fetching-data/1250/48 "2023-01-05T11:08:20Z")

</div>

Apps in production will typically connect to a HTTPS domain with an SSL certificate, and not an IP.

Localhost won’t work from your phone, because localhost on your phone is the phone itself, and localhost on the laptop is the laptop.

What you should do is create a Virtual Host. You can call it whatever you like. I’ll use `http://awesome.api:9000` as our example here.

On Linux or a Mac, we edit the hosts file with admin rights

```auto
sudo nano /etc/hosts

```

On Windows, the path is

```auto
C:\Windows\system32\drivers\etc\hosts

```

In there, we add the fake domain name, and point it to `127.0.0.1`:

```auto
127.0.0.1 awesome.api

```

Browsing to awesome.api now takes you to localhost. But we still need to proxy our device to the laptop. You can use Proxyman for that! (now also available for windows) [https://proxyman.io/](https://proxyman.io/).

Load it up, and proxyman will be running on port 9090, and will tell you the IP address (in my case it was `192.168.0.2`). In your phone Wifi settings, add the proxy settings, IP and port 9090.

Next open your phone browser and browse to [proxy.man/ssl](http://proxy.man/ssl), download and then install the Proxyman Certificate profile from Settings.

Finally, go into Settings \> General \> About \> Certificate Trust Settings, and switch on Enable Full Trust for the proxy man cert.

At this point you should be able to reach `http://awesome.api:9000` from your phone! 🕶

Just remember to turn proxy settings back off on your phone when not devving! 😛

---

<div class="post-metadata">

**Author:** ![tarsandor](https://avatars.discourse-cdn.com/v4/letter/t/a9adbd/32.png) [@tarsandor](https://forum.codewithmosh.com/u/tarsandor)\
**Post date:** [February 14, 2023, 5:56am UTC](https://forum.codewithmosh.com/t/react-native-part-2-networking-fetching-data/1250/49 "2023-02-14T05:56:59Z")

</div>

Dear somebody,  
That’s not working for me too. I used the correct IPv4 address, I did the same as Mosh and worked perfectly fine, but now there is no “Listings” screen when I tap on “Feed” button. Don’t appear for me any error or warning.  
Anybody who resolved the poblem? Please help us.  
Thankssss.

---

<div class="post-metadata">

**Author:** ![tarsandor](https://avatars.discourse-cdn.com/v4/letter/t/a9adbd/32.png) [@tarsandor](https://forum.codewithmosh.com/u/tarsandor)\
**Post date:** [February 18, 2023, 3:17pm UTC](https://forum.codewithmosh.com/t/react-native-part-2-networking-fetching-data/1250/50 "2023-02-18T15:17:34Z")

</div>

Well, I tried all of the possibilities, I used IPv6, './listings/, localhost.  
I also downloaded the zipped files and tried them, every time I got “problem”: “NETWORK\_ERROR”.  
I don’t know Mosh why didn’t figured out a better solution for backend, as I see a lot of people has the same problem. Is there somebody who resolved the problem?

The response of the request:  
Object {  
“config”: Object {  
“adapter”: [Function xhrAdapter],  
“baseURL”: “[http://192.168.0.10:9000/api](http://192.168.0.10:9000/api)”,  
“data”: undefined,  
“headers”: Object {  
“Accept”: “application/json”,  
},  
“maxContentLength”: -1,  
“method”: “get”,  
“params”: Object {},  
“timeout”: 0,  
“transformRequest”: Array [  
[Function transformRequest],  
],  
“transformResponse”: Array [  
[Function transformResponse],  
],  
“url”: “/listings”,  
“validateStatus”: [Function validateStatus],  
“xsrfCookieName”: “XSRF-TOKEN”,  
“xsrfHeaderName”: “X-XSRF-TOKEN”,  
},  
“data”: null,  
“duration”: 69619,  
“headers”: null,  
“ok”: false,  
“originalError”: [Error: Network Error],  
“problem”: “NETWORK\_ERROR”,  
“status”: null,  
}

---

<div class="post-metadata">

**Author:** ![luisdiego](https://avatars.discourse-cdn.com/v4/letter/l/ac8455/32.png) [@luisdiego](https://forum.codewithmosh.com/u/luisdiego)\
**Post date:** [June 17, 2024, 5:04pm UTC](https://forum.codewithmosh.com/t/react-native-part-2-networking-fetching-data/1250/52 "2024-06-17T17:04:31Z")

</div>

Thanks Jonasliu90, this solution worked for me!

[Previous page](https://forum.codewithmosh.com/t/react-native-part-2-networking-fetching-data/1250.md?page=2)
