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

我们两清 提交于 2019-11-28 06:54:49

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);

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).

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.

Possibly two things:

  1. Did you import { Link } from 'react-router-dom' in your Nav.js file?

  2. Possibly Spelling/Capitalization error: You export export default routes (lower case) but import Routes from './../routes (upper case).

The order in which include the component is also important in your index.js file. BrowserRouter, Provider then the App.

ReactDOM.render(
  <BrowserRouter>
      <Provider store={store}>
          <App />
      </Provider>
  </BrowserRouter>,
  document.getElementById('app')
);

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!