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

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 … Read more

Using Jest to test a Link from react-router v4

You can wrap your component in the test with the StaticRouter to get the router context into your component: import React from ‘react’; import renderer from ‘react-test-renderer’; import { Link } from ‘react-router-dom’; import { StaticRouter } from ‘react-router’ test(‘Link matches snapshot’, () => { const component = renderer.create( <StaticRouter location=”someLocation” context={context}> <Link to=”#” /> … Read more

How to nest routes in React Router v4?

Best pattern I have found so far. // main app <div> // not setting a path prop, makes this always render <Route component={AppShell}/> <Switch> <Route exact path=”https://stackoverflow.com/” component={Login}/> <Route path=”/dashboard” component={AsyncDashboard(userAgent)}/> <Route component={NoMatch}/> </Switch> </div> I can just keep nesting this inside a component and everything works nice including hmr(If using webpack, dont forget to … Read more

react-router (v4) how to go back?

I think the issue is with binding: constructor(props){ super(props); this.goBack = this.goBack.bind(this); // i think you are missing this } goBack(){ this.props.history.goBack(); } ….. <button onClick={this.goBack}>Go Back</button> As I have assumed before you posted the code: constructor(props) { super(props); this.handleNext = this.handleNext.bind(this); this.handleBack = this.handleBack.bind(this); // you are missing this line }

React Router 4 Nested Routes not rendering

This behaviour happens because have an exact attribute mentioned on the parent route <Route exact path=”https://stackoverflow.com/” component={Landing} /> So what happens is that react-router sees a path /test to match and then tries to match it starting from the top level. and it sees two routes one is exactly / and other is /contribute. None … Read more

Detect change in query param react-router-dom v4.x and re-render component

React router from v4 onwards no longer gives you the query params in its location object. The reason being There are a number of popular packages that do query string parsing/stringifying slightly differently, and each of these differences might be the “correct” way for some users and “incorrect” for others. If React Router picked the … Read more