I have a submenu with Routes in my /About.
This submenu is called AboutMenu and is present at all pages under /About like => /About/Company and /About/Info.
An exercise example shows <Route component={AboutMenu} />
with activeStyle={match.isExact && selectedStyle}>
and i just used <AboutMenu />
and added exact
to my AboutMenu Links instead.
Why would you use <Route component={AboutMenu} />
instead of just rendering <AboutMenu />
?
export const AboutMenu = (props) => {
return (
<div>
<li>
<NavLink exact to='/About' activeStyle={ activeStyle}> Company </NavLink>
</li>
<li>
<NavLink to='/About/History' activeStyle={activeStyle}> History </NavLink>
</li>
<li>
<NavLink to='/About/Vision' activeStyle={activeStyle}> Vision </NavLink>
</li>
</div>
)
}
about section
const About = (props) => {
return(
<Template>
<AboutMenu />
{/* OR <Route component={AboutMenu} /> ?*/}
<Route exact path='/About' component={Company} />
<Route path='/About/History' component={History} />
<Route path='/About/Vision' component={Vision} />
</Template>
)
}
export default About
React Router DOM enables you to implement dynamic routing in a web app. Unlike the traditional routing architecture in which the routing is handled in a configuration outside of a running app, React Router DOM facilitates component-based routing according to the needs of the app and platform.
Index Route - A child route with no path that renders in the parent's outlet at the parent's URL. Layout Route - A parent route without a path, used exclusively for grouping child routes inside a specific layout.
React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL.
React Router Dom is used to build single-page applications i.e. applications that have many pages or components but the page is never refreshed instead the content is dynamically fetched based on the URL. This process is called Routing and it is made possible with the help of React Router Dom.
<Route>
with no path
will always match. So, both
<AboutMenu />
and
<Route component={AboutMenu} />
will result in the AboutMenu
component always rendered. That is, in this particular case you can use either of them interchangeably.
However, there is a difference between the two in cases where the <Switch>
component is used. Look at this example:
<Switch>
<Route path="/" exact component={Home}/>
<Route path="/will-match" component={WillMatch}/>
<Route component={NoMatch}/>
</Switch>
Here,
<Route component={NoMatch}/>
comes last, and it will only match if no routes matched before it, acting like the default case
of the traditional switch
statement in many programming languages. It will not match if one of the two routes above it matches, and subsequently the NoMatch
component will not be rendered. If instead the last route there was just
<NoMatch />
the NoMatch
component would always be rendered.
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