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:
JSFIDDLE: https://jsfiddle.net/Lp3gzott/ (same code but babelified)
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.
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.
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.
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 .
I found the solution in react-router documentation. According to Server Rendering Guide:
match({ routes, location: req.url }, (err, redirectLocation, renderProps) => { renderToString(<RouterContext {...renderProps} /> })
Notice RouterContext
instead of Router
and lack of history
field in match
params
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With