React Router work on reload, but not when clicking on a link
Asked Answered
P

9

42

I have setup the React with react-router version 4. The routing works when I enter the URL directly on the browser, however when I click on the link, the URL changes on the browser (e.g http://localhost:8080/categories), but the content don't get updated (But if I do a refresh, it gets updated).

Below is my setup:

The Routes.js setup as follows:

import { Switch, Route } from 'react-router-dom';
import React from 'react';

// Components
import Categories from './containers/videos/Categories';
import Videos from './containers/videos/Videos';
import Home from './components/Home';

const routes = () => (
  <Switch>
    <Route exact path="/" component={Home}/>
    <Route path="/videos" component={Videos}/>
    <Route path="/categories" component={Categories}/>
  </Switch>
);

export default routes;

The link I use in Nav.js are as follows:

<Link to="/videos">Videos</Link>
<Link to="/categories">Categories</Link>

The App.js is as follows:

import React from 'react';
import './app.scss';
import Routes from './../routes';
import Nav from './Nav';

class AppComponent extends React.Component {

  render() {
    return (
      <div className="index">
        <Nav />
        <div className="container">
          <Routes />
        </div>
      </div>
    );
  }
}

AppComponent.defaultProps = {
};

export default AppComponent;
Petuntse answered 4/6, 2017 at 16:38 Comment(4)
Where's your BrowserRouterEggers
Did you get your problem solved? I have exactly the same issue and I'm interested in your final solution.Thackeray
@Thackeray The problem I had was putting in the <BrowserRouter> in the wrong place. The correct would be as in this pastebin: pastebin.com/M9hU4Bg4Petuntse
@SuthanBala - your latest commet saved me - after 6Hrs (?!) of spinning in circles. I was not using BrowseRouter at all ... just Router. Jeez ...Hymn
J
15

Wrapping your component with withRouter should do the job for you. withRouter is needed for a Component that uses Link or any other Router props and doesn't receive the Router props either directly from Route or from the Parent Component

Router Props are available to the component when its called like

<Route component={App}/>

or

<Route render={(props) => <App {...props}/>}/>

or if you are placing the Links as direct children of Router tag like

<Router>
     <Link path="/">Home</Link>
</Router>

In case when you wish to write the child content within Router as a component, like

<Router>
     <App/>
</Router>

The Router props won't be available to App and hence, you could pass call it using a Route like

<Router>
     <Route component={App}/>
</Router>

However withRouter comes in Handy when you want to provide the Router props to a highly nested component. Check this solution

import {withRouter} from 'react-router'

class AppComponent extends React.Component {

  render() {
    return (
      <div className="index">
        <Nav />
        <div className="container">
          <Routes />
        </div>
      </div>
    );
  }
}

AppComponent.defaultProps = {
};

export default withRouter(AppComponent);
Jigging answered 4/6, 2017 at 17:34 Comment(4)
still no luck :(Petuntse
Nvm, I had the browserhistory in the wrong location. Adjusting that fixed it.Petuntse
where did you adjust it, i'm have same problem, can you post a full solution?Clara
If using connect from react-redux, then wrap the withRouter around that connect. ex: export default withRouter(connect(mapStateToProps)(App)). Solution for that here https://mcmap.net/q/392075/-routes-are-not-navigating-when-react-v15-5-setup-with-react-redux-v5-isCarrara
A
19

I would go through your components and make sure you have only one <Router> ... </Router>. Also -- make sure you have a <Router>...</Router> There may be cases when you'd use more than one, but if you accidentally have nested routers (because you were hacking quickly and forgot to remove one when you were moving it around to all kinds of places ;-) - it could cause an issue.

I would try

import {
  BrowserRouter as Router,
}  from 'react-router-dom'

// Other Imports

...

return (
  <Router>
    <div className="index">
      <Nav /> <!-- In this component you have <Links> -->
      <div className="container">
        <Routes />
      </div>
    </div>
  </Router>
);

In your top most component (App.js).

Arluene answered 14/9, 2017 at 6:42 Comment(3)
Double <Router/> was the issue in my case.Unpleasant
switching to Browserrouter instead of just Router worked for me! thanksSovereign
This solution works for me. I used double Router in my app.Aggy
J
15

Wrapping your component with withRouter should do the job for you. withRouter is needed for a Component that uses Link or any other Router props and doesn't receive the Router props either directly from Route or from the Parent Component

Router Props are available to the component when its called like

<Route component={App}/>

or

<Route render={(props) => <App {...props}/>}/>

or if you are placing the Links as direct children of Router tag like

<Router>
     <Link path="/">Home</Link>
</Router>

In case when you wish to write the child content within Router as a component, like

<Router>
     <App/>
</Router>

The Router props won't be available to App and hence, you could pass call it using a Route like

<Router>
     <Route component={App}/>
</Router>

However withRouter comes in Handy when you want to provide the Router props to a highly nested component. Check this solution

import {withRouter} from 'react-router'

class AppComponent extends React.Component {

  render() {
    return (
      <div className="index">
        <Nav />
        <div className="container">
          <Routes />
        </div>
      </div>
    );
  }
}

AppComponent.defaultProps = {
};

export default withRouter(AppComponent);
Jigging answered 4/6, 2017 at 17:34 Comment(4)
still no luck :(Petuntse
Nvm, I had the browserhistory in the wrong location. Adjusting that fixed it.Petuntse
where did you adjust it, i'm have same problem, can you post a full solution?Clara
If using connect from react-redux, then wrap the withRouter around that connect. ex: export default withRouter(connect(mapStateToProps)(App)). Solution for that here https://mcmap.net/q/392075/-routes-are-not-navigating-when-react-v15-5-setup-with-react-redux-v5-isCarrara
B
5

There should be only one ROUTER in the whole app, which I think if your head file is App.js, then the ROUTER should wrap the whole App.js component.

Barbarian answered 7/10, 2019 at 22:41 Comment(0)
V
3

I was having the exact same issue, but the cause of mine was much simpler.

In my case I had a space at the end of the URL string:

<Link to={ "/myentity/" + id + "/edit " } >Edit</Link>

Wouldn't work when I clicked on the link, but the URL would update in the address bar. Tapping on the browser address bar and hitting the enter key would then work correctly.

Removing the space fixed it:

<Link to={ "/myentity/" + id + "/edit" } >Edit</Link>

Pretty obvious I guess, but easy to overlook. A few hairs were pulled before I noticed (and clearly I ended up here), hope it saves someone else a few hairs.

Veroniqueverras answered 26/11, 2018 at 6:9 Comment(1)
Thank you! Very easily overlookedSharenshargel
S
1

If anyone is using the history package for navigation and if you are using the react-router v5, then you may have to downgrade the history package to 4.10.1 until the history package developers issue a new release with the fix. As of writing this, the latest release version of history package is v5.0.1, and if you see a new release than this, you can try with that version first to see if it works, before doing the downgrade to 4.10.1

Strop answered 13/10, 2021 at 20:16 Comment(0)
R
0

I was facing this because I have not installed the react-router in the application (package.json) dependencies...

Ringworm answered 4/6, 2019 at 0:4 Comment(0)
R
0

You should not wrap the nav-bar where your are stored in or

I also had the same issue before. The mistake was the I wrapped my navbar component inside a you should use in the main component only (App.js)

Repress answered 18/3, 2021 at 4:15 Comment(0)
P
0

The answer by Hanly Nadackal is where I found my solution.

As referenced in an issue at the history package repository, v5 should not be used for react-router v5. https://github.com/remix-run/history/issues/893#issuecomment-905959905

Downgrading history at my package.json to ^4.10.1 fixed the problem.

Pearl answered 19/11, 2021 at 3:32 Comment(0)
L
0

React router dom v6 you need to wrap Navbar component inside router

      <Router>
        <Nav />
        <Routes>
          <Route exact path="/" element={<LandingPage />} />
          <Route exact path="projects" element={<Projects />} />
          <Route exact path="about" element={<About />} />
          <Route exact path="blog" element={<Blog />} />
        </Routes>
      </Router>
      <Footer />
Lycopodium answered 26/6, 2022 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.