Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why <Route component={Menu}/> instead of <Menu />?

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
like image 751
Hyrule Avatar asked Jun 10 '17 01:06

Hyrule


People also ask

Does React handle routing?

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.

What is index in Route?

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.

How does React Route work?

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.

What does React Router DOM does?

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.


1 Answers

<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.

like image 182
m1kael Avatar answered Sep 23 '22 23:09

m1kael