4

I am using React router to change routes. This works well when I develop locally.

My router code looks like this and everything works perfectly.

<Router>
    <Route exact path="/" render={(routeProps)=> <Homepage {...routeProps} />}/>
    <Route path="/graph" render={(routeProps)=> <AboutPage {...routeProps} />}/>            
</Router>

The issue I am having is when I deploy it to my github page, http://exampleuser.github.io/react-project.

As the project is in a react-project folder this "/" refers to the actual route which is http://exampleuser.github.io/

Can anyone advise how the path should look so it works when deployed?

5
  • Have you tried using <Switch> ? import <Switch> from 'react-router-dom' and then wrap everything in a <div> and then wrap all <Route>'s inside <Switch> Commented Dec 1, 2017 at 20:58
  • I have, but i don't think it is to do with Switch - I believe it is to do with the route once I have deployed as it is no longer "/" Commented Dec 1, 2017 at 21:15
  • put "/" <Route>in the last not the first? Commented Dec 1, 2017 at 21:16
  • are you, by any chance using create react app? Commented Dec 1, 2017 at 21:56
  • @randomguy04 I am Commented Dec 1, 2017 at 22:15

2 Answers 2

8

You can use the basename prop in your router, just make sure you only use that in production and not in development (you could use environment variables for that) your router should look like this:

<Router basename="your-react-project">
  {/* routes */}
</Router>

If you are using create_react_app, you can just use the env variable process.env.PUBLIC_URL like this (which is empty en development so it will work fine for bot dev and production):

<Router basename={process.env.PUBLIC_URL}>
  {/* routes */}
</Router>

I actually have a project running with this configuration and works just fine.

Sign up to request clarification or add additional context in comments.

4 Comments

so it actually works fine when it is the project at the root of my github repo, but it is in a subfolder i.e - http://exampleuser.github.io/react-project
have you tried the first approach? where you can set the basename manually?
yeah man, just deployed there and it works! the baseman works a treat! Green tick coming up! side question, where do u set the process.env.PUBLIC_URL variable?
glad to hear that. You don't have to set it yourself, they briefly explain how it works here github.com/facebookincubator/create-react-app/blob/master/…
0

it's an easy fix I came across the same issue here is how I fixed it.

1- go to your root directory in your cPanel or server if you are using apache. and create a .htaccess file

open the file and add the following code to it.

#jerryUrena is awesome RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L]

after this, your website should work the same way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.