Using fetch in React Native to access an API from your local environment

Andrew Zimmer
1 min readDec 16, 2020

I’m going to make this quick. I’ve recently been trying to make a fetch request from my react native app to a Rails API that I’m running locally and I was not getting anywhere. After about an hour of looking, I found the answer! So I’m going to share that with you so you don’t have to have the headache I had.

So if you are running an android emulator (I’m using Android Studio) you need to replace the ‘localhost’ on your address your using and replace it with ‘10.0.2.2’. It’s that easy!

http://localhost:3000/v1/users

remove the ‘localhost’ and replace it with the ‘10.0.2.2’.

http://10.0.2.2:3000/v1/users

If you are using your phone, it’s a little more complex then that. I have seen that you need to replace ‘localhost’ with your ip address. But that’s all I know.

I hope this helps someone. Thanks for reading and good luck coding.

--

--

Andrew Zimmer

I'm a software developer and recent graduate, with a love for exploring and developing creative solutions to complex problems.