I am trying to implement React Router Breadcrumbs for v4
Following are my routes:
const routes = {
'/': 'Home',
'/page1': 'Page 1',
'/page2': 'Page 2'
};
I could put the breadcrumbs using this library in my application, however I am having following questions:
Que. #1:
When I click on Home
in my breadcrumbs, I can see the URL changes to http://localhost:8080
However, browser still shows the same page I am on.
Que. #2:
When I navigate to Page2 from Page1, url
changes from http://localhost:8080/page1
to http://localhost:8080/page2
.
So the breadcrumbs shown changes to Home / Page 2
instead of changing like Home / Page 1 / Page 2
I know this may be because the url
just has /page2
after hostname. But, can I achieve the display like: Home / Page 1 / Page 2
?
Below is the code in my main App.jsx
:
<Router>
<div>
<Link to="/"><div className="routerStyle"><Glyphicon glyph="home" /></div></Link>
<Route exact path="/" component={LandingPage}/>
<Route path="/page1" component={Page1}/>
<Route path="/page2" component={Page2}/>
</div>
</Router>
and if I use like belowto cater for breadcrumbs, then my page2 gets rendered below page1 stuff:
<Router>
<div>
<Link to="/"><div className="routerStyle"><Glyphicon glyph="home" /></div></Link>
<Route exact path="/" component={LandingPage}/>
<Route path="/page1" component={Page1}/>
<Route path="/page1/page2" component={Page2}/>
</div>
</Router>
Answer:
Que. #1: No need to wrap <Breadcrumbs ..../>
element inside <Router>
element inside each Component of application. This may be because, inclusion of <Router>
element inside each Component leads to "nesting" of Router
elements (note we have Router
tag in landing page as well); which does not work with react router v4
.
Que. #2: Refer to answer formally marked here (answered by palsrealm below)