Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-router: `<Link to=...>` does't trigger navigation change

I've got code similar to following:

var browserHistory = ReactRouter.browserHistory;
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var Link = ReactRouter.Link;

class App extends React.Component {
  render() {
    return (
      <div>
        <h1>Here is content:</h1>
        {this.props.children}
        <Link to="/Welcome">Welcome</Link> |
        <Link to="/Login">Login</Link>
        <a href="/">REFERENCE LINK</a>
      </div>
    );
  }
}

class Welcome extends React.Component {
  render() {
    return (
      <div>
        <h1>No hejaaaa - welcome</h1>
      </div>
    );
  }
}

class Login extends React.Component {
  render() {
    return (
      <div>
        <h1>No hejaaaa - Login</h1>
      </div>
    );
  }
}

const Routes = (
  <Router history={browserHistory}>
    <Route path="/" component={App}>
      <Route path="Welcome" component={Welcome}/>
      <Route path="Login" component={Login}/>
      <Route path="*" component={Welcome}/>
    </Route>
  </Router>
);

// init file:
var RouterContext = ReactRouter.RouterContext
var match = ReactRouter.match

match({
  routes: Routes,
  location: document.location.pathname
}, (err, redirectLocation, renderProps) => {
  ReactDOM.render(<RouterContext {...renderProps} />, document.querySelector('#app'));
});

Markup is generated correctly, but the problem is: Clicking in Links doesn't work at all. I am doing something wrong?

My libs:

  • "react": "0.14.7",
  • "react-dom": "0.14.7",
  • "react-router": "2.0.0"

JSFIDDLE: https://jsfiddle.net/Lp3gzott/ (same code but babelified)

like image 452
Cezary Daniel Nowak Avatar asked Feb 11 '16 19:02

Cezary Daniel Nowak


People also ask

What is difference between link and navigate in react?

Note also that Link is a React component and as such it must be rendered into the DOM as part of the return from a React component, whereas the navigate function is a function and can be used in callbacks.

Which hook of react-router-dom will allow you to navigate programmatically to a different route?

There are two ways to programmatically navigate with React Router - <Navigate /> and navigate() . You can get access to Navigate by importing it from the react-router-dom package and you can get access to navigate by using the custom useNavigate Hook.

How does react router link work?

React-Router matches the URL and loads up the component for that particular page. Everything happens so fast, and seamlessly, that the user gets a native app-like experience on the browser. There is no flashy blank page in between route transitions.

Can I pass props with link react router?

To recap, if you need to pass data from Link through to the new component that's being rendered, pass Link s a state prop with the data you want to pass through. Then, to access the Link s state property from the component that's being rendered, use the useLocation Hook to get access to location. state .


2 Answers

I found the solution in react-router documentation. According to Server Rendering Guide:

  • In back end set:
match({ routes, location: req.url }, (err, redirectLocation, renderProps) => {
  renderToString(<RouterContext {...renderProps} />
})

Notice RouterContext instead of Router and lack of history field in match params

  • In front-end:
match({ history, routes }, (error, redirectLocation, renderProps) => {
  ReactDOM.render(<Router {...renderProps} />, mountNode)
})

Notice lack of location param for match

  • In routes file:

    export <Route instead of <Router


Error React attempted to reuse markup in a container but the checksum was invalid. is not showing up again.

Links are working like a charm!

like image 142
Cezary Daniel Nowak Avatar answered Oct 07 '22 17:10

Cezary Daniel Nowak


match() is a Server side rendering construct, its intentional static, because on the server you only ever respond to a single route at a time. On the client you want to actually render a Router component

ReactDOM.render((
  <Router>
    { Routes }
  </Router>
), document.querySelector('#app'))

Your, markup mismatch is probably due to a different issue, and you might want to check out one of the many "universal react" starters.

like image 42
monastic-panic Avatar answered Oct 07 '22 17:10

monastic-panic