英文:
How does React-Router determine which path to follow?
问题
以下是您要翻译的内容:
"作为参考,Layout
返回的是一个 Link
标签,如下所示:
<Link to="about">About</Link>
因此,它是一个相对路径。所以当我点击 "about"
链接时,它会获取相对于当前路径的 URL,因此我的最终 URL 将是 "http://localhost:3000/about"
,这意味着 react-router
首先会寻找路径为 "/"
的路由,然后再寻找路径为 "about"
的路由。
但是在下面的代码中,我将 "about"
路由放在了具有路径 "/"
的路由之外,尽管如此,我的 About
组件仍然被渲染。
为什么会发生这种情况?
我对 react-router
如何确定要跟随哪个路径以及因此要渲染哪个组件的主题感到困惑。"
请注意,我只提供了内容的翻译,没有包括问题或其他内容。
英文:
For reference, the Layout
is returning a Link
tag which is as follows:
<Link to="about">About</Link>
Therefore it is a relative path. So when I'm clicking the "about"
link, it is getting a URL relative to the current path so my final URL would be "http://localhost:3000/about"
that means that react-router
will first look for the route that has a path of "/"
, and then look for a route that has a path of "about"
.
But in the code below, I've placed the "about"
route outside the route that has the path "/"
and despite this, my About
component is rendering.
Why is that happening?
I'm quite confused on the topic of how react-router
determines which path to follow and thus which component will be rendered.
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { BrowserRouter, Routes, Route } from "react-router-dom"
import Home from './Home'
import About from './About';
import Layout from './components/Layout';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<BrowserRouter>
<Routes>
<Route path='/' element={<Layout />}>
<Route index element={<Home />} />
<Route path='drinks' element={<GetDrinks />} />
<Route path='drinks/:drink' element={<FetchDrink />} />
<Route path='host' element={<HostLayout />}>
<Route path='dashboard' index element={<Dashboard />} />
<Route path='income' element={<Income />} />
<Route path='reviews' element={<Reviews />} />
</Route>
</Route>
<Route path='about' element={<GetDrinks />} />
</Routes>
</BrowserRouter>
);
答案1
得分: 1
以下是翻译好的部分:
"All routes are assumed to start from root, e.g. /
. So given the About page is a root level route, it will be rendered relative to the root, e.g. /about
."
<BrowserRouter>
<Routes>
<Route path='/' element={<Layout />} >
<Route index element={<Home />} />
<Route path='drinks' element={<GetDrinks />} />
<Route path='drinks/:drink' element={<FetchDrink />} />
<Route path='host' element={<HostLayout />} >
<Route path='dashboard' index element={<Dashboard />} />
<Route path='income' element={<Income />} />
<Route path='reviews' element={<Reviews />} />
</Route>
</Route>
<Route path='about' element={<GetDrinks />} /> // Matches "/about"
</Routes>
</BrowserRouter>
is the same as explicitly specifying the full paths:
<BrowserRouter>
<Routes>
<Route path='/' element={<Layout />} >
<Route index element={<Home />} />
<Route path='/drinks' element={<GetDrinks />} />
<Route path='/drinks/:drink' element={<FetchDrink />} />
<Route path='/host' element={<HostLayout />} >
<Route path='/host/dashboard' index element={<Dashboard />} />
<Route path='/host/income' element={<Income />} />
<Route path='/host/reviews' element={<Reviews />} />
</Route>
</Route>
<Route path='/about' element={<GetDrinks />} />
</Routes>
</BrowserRouter>
Being able to use relative paths in the routing configuration is basically allowing you to write more succinct code and make maintaining the code easier if you want to move routes around. For example, if you wanted to move all the /host/*
routes to /guest/*
, you only need to change the parent layout route from /host
to /guest
, and all the nested routes automatically get the correct path.
Note: This route matching should not be confused with the "absolute"-ness vs "relative"-ness of navigation links/targets. Using an absolute link target <Link to="/about">About</Link>
, the link will always navigate to "/about" regardless of where the link is rendered within the routing tree. But using a relative link target <Link to="about">About</Link>
, it will only append relative to the path for whatever the path is where the link is rendered. For example:
- When
<Link to="about">About</Link>
is rendered in the rootLayout
component rendered on "/", then the link correctly navigates to "/about". - If/when
<Link to="about">About</Link>
is rendered more deeply, like in theHostLayout
rendered on "/host", the result would be a navigation to "/host/about". Or if rendered inDashboard
, the result would be "/host/dashboard/about".
英文:
All routes are assumed to start from root, e.g. "/"
. So given the About page is a root level route, it will be rendered relative to the root, e.g. "/about"
.
<BrowserRouter>
<Routes>
<Route path='/' element={<Layout />}>
<Route index element={<Home />} />
<Route path='drinks' element={<GetDrinks />} />
<Route path='drinks/:drink' element={<FetchDrink />} />
<Route path='host' element={<HostLayout />}>
<Route path='dashboard' index element={<Dashboard />} />
<Route path='income' element={<Income />} />
<Route path='reviews' element={<Reviews />} />
</Route>
</Route>
<Route path='about' element={<GetDrinks />} /> // <-- Matches "/about"
</Routes>
</BrowserRouter>
is the same as explicitly specifying the full paths
<BrowserRouter>
<Routes>
<Route path='/' element={<Layout />}>
<Route index element={<Home />} />
<Route path='/drinks' element={<GetDrinks />} />
<Route path='/drinks/:drink' element={<FetchDrink />} />
<Route path='/host' element={<HostLayout />}>
<Route path='/host/dashboard' index element={<Dashboard />} />
<Route path='/host/income' element={<Income />} />
<Route path='/host/reviews' element={<Reviews />} />
</Route>
</Route>
<Route path='/about' element={<GetDrinks />} />
</Routes>
</BrowserRouter>
Being able to use relative paths in the routing configuration is basically allowing you to write more succinct code and make maintaining the code easier if you want to move routes around, i.e. if you wanted to move all the "/host/*"
routes to "/guest/*"
you only need to change the parent layout route from "/host"
to "/guest"
and all the nested routes automagically get the correct path.
Note:
This route matching should not be confused with the "absolute"-ness vs "relative"-ness of navigation links/targets. Using an absolute link target <Link to="/about">About</Link>
the link will always navigate to "/about"
regardless of where the link is rendered within the routing tree, but using a relative link target <Link to="about">About</Link>
will only append relative to the path for whatever the path is where the link is rendered.
For example:
- When
<Link to="about">About</Link>
is rendered in the rootLayout
component rendered on"/"
then the link correctly navigates to"/about"
. - If/when
<Link to="about">About</Link>
is rendered more deeply, like in
theHostLayout
rendered on"/host"
, the result would be a
navigation to"/host/about"
. Or if rendered inDashboard
, the result would be"/host/dashboard/about"
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论