Skip to content

React Router Not Working #1807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tamaccount opened this issue Mar 13, 2017 · 12 comments
Closed

React Router Not Working #1807

tamaccount opened this issue Mar 13, 2017 · 12 comments

Comments

@tamaccount
Copy link

tamaccount commented Mar 13, 2017

I am having trouble using React Router to create another static page within my app.

Here is what my render method looks like:

ReactDOM.render(
 <Router history={hashHistory}>
  <Route path="/" component={Home}>
     <Route path="material" component={Material}/>
   </Route>
 </Router>,
 document.getElementById('root')
);

When I try to visit localhost:8080/material, the url redirects to localhost:8080/material#/ and I the page just loads Home instead of Material. There is no way for me to access my Material Component.

The same error occurs when I reconfigure the render method to look like this:

ReactDOM.render(
 <Router history={hashHistory}>
  <Route path="/" component={Home}/>
  <Route path="/material" component={Material}/>
 </Router>,
 document.getElementById('root')
);

What do I have to do to get access to different routes?
edit: forgot question mark

@danieldram
Copy link

Which version of react-router are you using?

@tamaccount
Copy link
Author

My package.json says 3.0.2

@viankakrisna
Copy link
Contributor

viankakrisna commented Mar 13, 2017

You need to go to localhost:8080/#/material to access your material component because you are using hashHistory.

@tamaccount
Copy link
Author

Thank you for the help.

Is there a way to eliminate having to use the hashHistory or history altogether?

I believe when I was not feeding the history prop, no pages would load and I received an error in the console saying "Cannot read property 'getCurrentLocation' of undefined".

@gaearon
Copy link
Contributor

gaearon commented Mar 13, 2017

If you want to use the browser history, you should pass the browserHistory instead.

import { browserHistory } from 'react-router';

ReactDOM.render(
 <Router history={browserHistory}>

I hope this helps!

@gaearon gaearon closed this as completed Mar 13, 2017
@tamaccount
Copy link
Author

Thank you everyone for the help!

@danieldram
Copy link

Be careful if you decide to upgrade to 4.0. I was having similar issue as you but with hashHistory and BrowserHistory being undefined, it is because 4.0 injects the history into the props you pass down to your routes. Lock down your ver if you don't want things to break on a deploy to prod :)

@alisonzthu
Copy link

@danieldram may I ask what do you mean by "lock down your ver" plz?

@Timer
Copy link
Contributor

Timer commented Apr 11, 2017

@alisonzthu he's probably referring to not using * in your package.json, if it reads something like ^3.0.0 you're fine (you should never use *).

To truly lock down your versions, you must use Yarn.

@alisonzthu
Copy link

@Timer, I see. Thanks for the explanation!

@bibekg
Copy link

bibekg commented Jul 6, 2017

I've found that using browserHistory prevents me from navigating directly to custom routes (going straight to example.com/mypath in-browser doesn't work). Is there an easy way around this shortcoming of browserHistory?

@Timer
Copy link
Contributor

Timer commented Jul 6, 2017

That is a question better asked in the react router community. That said, make sure your production configuration serves index.html for all routes not explicitly matched.

@lock lock bot locked and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants