Skip to content

React Router

一句话解释

React Router 是库 那个启用导航之间不同 pages/views in React 单个-page application.

用大白话说

React Router 是 de facto routing 库 for React. 它 lets 你映射 URL paths 到组件, 所以导航到 /about 渲染你的关于组件, /users/42 renders a user profile, etc. — all without full page reloads.

Core components: <BrowserRouter> wraps the app, <Routes> contains route definitions, <Route path="/关于" 元素={<关于 />} /> maps a path to a component, <链接 到="/关于"> creates navigation links, and useParams() / useNavigate() are Hooks for reading route params and navigating programmatically.

React Router keeps the URL in sync with what's rendered, supports nested routes, dynamic parameters, query strings, and lazy-loaded route components. It is a separate package (react-router-DOM) — React itself does not include routing.

示例

jsx
<BrowserRouter>
  <Routes>
    <Route path="/" element={<Home />} />
    <Route path="/about" element={<About />} />
    <Route path="/users/:id" element={<UserProfile />} />
  </Routes>
</BrowserRouter>

Three routes 是定义. 导航到 /用户/42 渲染 UserProfile, 哪个能 读取 id param 使用 useParams().

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表