Home > Software design >  How do I get my physical device running React Native on Expo Go to communicate with my app's ra
How do I get my physical device running React Native on Expo Go to communicate with my app's ra

Time:04-07

I am currently trying to run my react-native/rails app on my phone for testing purposes. I can run my sign in and log in screens fine because they do not communicate with my server until the user info is entered. When running my server i use: $ rails s --binding=0.0.0.0

I do not receive any errors other than knowing my server is not being communicated with. This all works fine on my Android Studio Emulator as well.

// one of my fetch GET requests

export function requestCurrentUser(username, auth_token) {
  return function action(dispatch) {
    const request = fetch(`'http://10.0.2.2:3000'/users/${username}`, {
      method: 'GET',
        headers: {
          "Authorization": auth_token
        }
    });
    return request.then(
      response => response.json(),
      err => console.log("user error"),
    )
    .then(
      json => dispatch(receiveCurrentUser({id: json.id, email: json.email, username: json.username})),
      err => console.log("user json error"),
    );
  }
}

I've tried changing my Phone IP settings to a 10.0.2.2 Gateway, and using my phone's IP in my fetch request. I feel like I am missing something conceptually. Thanks in advance.

CodePudding user response:

In fetch request you need to use the IP from the machine that are running the rails server, probably your notebook and use the same network to connect your app and your rails backend api. In order to test, you can try directly access your api in your phone browser accessing http://IP_FROM_RAILS_MACHINE:3000

  • Related