Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Router : What is the purpose of IndexRoute?

I don't understand what the purpose of using an IndexRoute and IndexLink. It seems that in any case the code below would of selected the Home component first unless the About path was activated.

<Route path="/" component={App}>   <IndexRoute component={Home}/>   <Route path="about" component={About}/> </Route> 

vs

<Route path="/" component={App}>   <Route path="home" component={Home}/>   <Route path="about" component={About}/> </Route> 

What's the advantage/purpose here of the first case?

like image 476
Nick Pineda Avatar asked Sep 22 '15 01:09

Nick Pineda


People also ask

Why do I need BrowserRouter?

BrowserRouter is used for doing client side routing with URL segments. You can load a top level component for each route. This helps separate concerns in your app and makes the logic/data flow more clear.

Why we use HashRouter in react?

HashRouter: When we have small client side applications which doesn't need backend we can use HashRouter because when we use hashes in the URL/location bar browser doesn't make a server request. BrowserRouter: When we have big production-ready applications which serve backend, it is recommended to use <BrowserRouter> .

How does react router dom link work?

A <Link> is an element that lets the user navigate to another page by clicking or tapping on it. In react-router-dom , a <Link> renders an accessible <a> element with a real href that points to the resource it's linking to. This means that things like right-clicking a <Link> work as you'd expect.

What is useRoutes in react router dom?

The useRoutes hook is a first-class API for routing that lets you declare and compose your routes using JavaScript objects instead of React elements.


1 Answers

In the top example, going to / would render App with Home passed as a child. In the bottom example, going to / would render App with neither Home nor About being rendered, since neither of their paths match.

For older versions of React Router, more information is available on associated version's Index Routes and Index Links page. Starting in version 4.0, React Router no longer uses the IndexRoute abstraction to achieve the same goal.

like image 135
Michelle Tilley Avatar answered Oct 01 '22 12:10

Michelle Tilley