response-router-dom (v4) / :
import React, {Component} from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
const Quote = () => (
<h1>Kick butt and chew bubble gum</h1>
);
const OtherQuote = () => (
<h1>"Be yourself; everyone else is already taken."― Oscar Wilde</h1>
);
export class SubPage extends React.Component {
redirectToHomepage = () => {
this.props.history.push('/')
};
render() {
return (
<div id="main">
<button onClick={this.redirectToHomepage}>
Back Home
</button>
<div>
<Route path="/quote" component={Quote} />
<Route path="/otherQuote" component={OtherQuote} />
</div>
</div>
);
}
}
export class ReactRouterNesting extends React.Component {
render() {
return (
<Router>
<div>
<Route path="/" component={SubPage}/>
</div>
</Router>
)
}
}
The subpage will be displayed every time, since it will always correspond to the "/" route, but the remaining content of the page is based on the path. There is no need to embed the Router component in a subpage and support routes nested in this way, and not inside multiple routers, it will transmit the history through the details and allow you to advance the history through the details.
Hope this helps and good luck!
source
share