question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No use with react-router v4

See original GitHub issue

When I click the <Link> to redirect, the url in browser correctly change but nothing happen in the page.

Here is my code:

// Root.tsx
import * as React from 'react'
import createBrowserHistory from 'history/createBrowserHistory'
import {Link, Route, Switch} from 'react-router-dom'
import {Provider} from 'mobx-react'
import {Router} from 'react-router'
import {syncHistoryWithStore, RouterStore} from 'mobx-react-router'

const routing = new RouterStore()

const browserHistory = createBrowserHistory()

const history = syncHistoryWithStore(browserHistory, routing)

function PageHome () {
  return (
    <div>
      <h1>home</h1>
      <Link to='/404'>404</Link>
    </div>
  )
}

function Page404 () {
  return (
    <div>
      <h1>404</h1>
      <Link to='/'>home</Link>
    </div>
  )
}

export default class Root extends React.Component<void, void> {
  render () {
    return (
      <Provider routing={routing}>
        <Router history={history}>
          <Switch>
            <Route path='/' exact component={PageHome}/>
            <Route component={Page404}/>
          </Switch>
        </Router>
      </Provider>
    )
  }
}

// index.tsx
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import Root from './Root'
import {AppContainer} from 'react-hot-loader'
import './styles/base.styl'

const render = (Root: any) => {
  ReactDOM.render(
    <AppContainer>
      <Root/>
    </AppContainer>,
    document.getElementById('app')
  )
}

render(Root)

if (module.hot) {
  module.hot.accept('./Root', () => {
    const nextRoot = require('./Root').default
    render(nextRoot)
  })
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
robksawyercommented, Sep 27, 2017

For anyone having this issue, I ended up using @withRouter like so in my App.jsx and that seemed to do the trick.

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

@inject('store')
@withRouter
@observer
export default class App extends Component {
...
}
4reactions
alisd23commented, Apr 9, 2017

Let me know if there’s still issues. New version 4.0.1 is now out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React router v4 redirect when no match - Stack Overflow
For anybody arriving here looking for how to redirect if none of the routes matches: <Switch> // ... your routes <Route render={() ...
Read more >
React Router DOM v4 Tutorial (with Examples) - ITNEXT
In this tutorial we are going to get you started with react-router-dom using an example React application showing you how to use different...
Read more >
Handling 404 pages (catch all routes) with React Router v4
In this post you'll learn how to implement catch all routes for handling 404 pages with React Router v4.
Read more >
All About React Router 4 - CSS-Tricks
This post is going to dig into to React Router 4, how it's so different from previous React Router versions, and why that...
Read more >
A Simple React Router v4 Tutorial - Blog
Build a small single-page application using React Router v4 ... Here it is. No fancy styling, just a simple, functional website.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found