2

I am trying to configure react-navigation for a web app with react native. For that I set up the linking options on a NavigationContainer so that I can access my pages from a browser url, using this code :

const linking = {
  prefixes: ['http://localhost:8080/', 'http://localhost:8080', 'localhost:8080'],
  // prefixes: [prefix],
  config: {
    screens: {
      SignIn: "SignIn",
      SignUp: "SignUp",
      Landing: '*',
    },
  }
};

function AppContainer() {

  return ( 
    <NavigationContainer linking={linking} fallback={<Text>Loading...</Text>}>
      <AppStack.Navigator>
        <AppStack.Screen name="SignIn" component={SignInPage}/>
        <AppStack.Screen name="Landing" component={LandingPage}/>
        <AppStack.Screen name="SignUp" component={SignUpPage}/>
        <AppStack.Screen name="Home" component={HomePage}/>
      </AppStack.Navigator>
    </NavigationContainer>
  );
}

When I go to "http://localhost:8080/", I am redirected to "http://localhost:8080/SignIn" ( which is fine), and the app is working. The problem is that if I go from my browser to "http://localhost:8080/SignIn" I get "Cannot GET /SignIn", and the app is not working...

I am using these versions :

"@react-navigation/bottom-tabs": "^5.11.1",
"@react-navigation/native": "^5.8.9",
"@react-navigation/stack": "^5.12.5",
Pijean
  • 61
  • 7
  • You don't have a direct route to "http://localhost:8080/Signin". I would add it to the prefixes array – Qrow Saki Nov 15 '20 at 22:55
  • Adding "localhost:8080/Signin" or "http://localhost:8080/Signin" to the prefixes array didn't work – Pijean Nov 15 '20 at 23:23

1 Answers1

3

Found a solution on How to tell webpack dev server to serve index.html for any route.

I was using webpack-dev-server, which needs some configurations in the webpack.config.js to map all the url to / and serve the index.html .. These are the configurations :

devServer: {
port: 3000,
historyApiFallback: {
  index: 'index.html'
}

}

Adding the cli option --history-api-fallback also do the trick.

Pijean
  • 61
  • 7