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