👨‍💻
Hong's 前端笔记
  • 🌈About
  • JavaScript
    • 作用域
    • 闭包
    • this
      • bind()
    • 原型
      • Object.create()
      • 模拟“类”
      • ES6 Class
      • Function
    • 对象
    • 类型 & 值
      • 原生函数
      • 数组
      • 字符串
    • 异步
    • JavaScript 相关概念
      • Event Loop 事件循环
      • Prototype 原型
      • Context 执行上下文
      • this
      • Promise
    • JavaScript 常见问题
      • 手写实现
      • 看代码输出
  • React
    • 核心概念
    • 深入理解
      • State
      • 如何管理 State
      • React 渲染过程
      • 处理 DOM 事件
      • Pure 组件
      • Context
      • Key 属性
      • React.lazy()
      • Render Props
    • Hooks
      • useState
      • useRef
      • useEffect
        • Some details
      • 相关概念
      • 自定义 Hook
      • useEffect vs useLayoutEffect
    • React 相关问题
      • Class vs Function
  • React Libraries
    • Redux
      • Quick Start
      • 如何持久化 Redux 数据
      • 异步操作
      • 性能优化
      • Dive into immutability
    • Route v6
      • Quick Start
  • Reactivity
    • Immer
  • Vue
    • 深入理解
      • Composition vs Options
      • Reactivity
  • Network
    • Cookies
    • HTTP
    • HTTPS
    • CORS 跨域资源共享
    • 认证手段
    • 安全相关
    • 网络相关知识
  • Browser
    • DOM 操作
    • Events 事件
    • XHR & Fetch
    • 性能优化
    • HTML 相关概念
    • 浏览器相关概念
  • CSS
    • 盒模型
    • Layout 布局
    • Styles 样式
    • CSS 相关概念
    • CSS 相关技巧
      • 移动端适配
      • Flex
      • 动画
  • TypeScript
    • Quick Peek
    • Types in TypeScript
    • Narrowing
    • Functions in TypeScript
  • Workflow
    • Webpack
      • Webpack 概念
      • 资源管理
      • 管理输出
      • 开发环境配置
      • 生产环境配置
      • 优化代码运行性能
  • Others
    • 小程序与原生 Web 的区别
  • SSO
    • About
    • API Doc
    • 接入指南
Powered by GitBook
On this page
  • Route 要用标签包裹
  • 组件嵌套路由
  • Nav Link
  • No match case
  • Index Routes
  • Params
  • Use Location
  • Navigate
Edit on GitHub
  1. React Libraries
  2. Route v6

Quick Start

Route 要用标签包裹

element 替换了原来的 component 属性

<BrowserRoute>
	<Routes>
		<Route path='/xxx' element={<Component />}
		...
	</Routes>
</BrowserRoute>

组件嵌套路由

<BrowserRouter>
  <Routes>
    <Route path="/" element={<App />}>
      <Route path="expenses" element={<Expenses />} />
      <Route path="invoices" element={<Invoices />} />
    </Route>
  </Routes>
</BrowserRouter>

需要在父组件中放置 标签,来容纳嵌套的组件

<div>
  ... static contents...
  <Outlet />
</div>

Nav Link

如果想要 Link 在激活时有独特的样式,就是用 NavLink

<NavLink
  style={({ isActive }) => {
    return {
      display: "block",
      margin: "1rem 0",
      color: isActive ? "red" : ""
		    };
  }}
  to='/xxx'
/>

No match case

设置一个 fallback 选项

<Route path="/" element={<App />}>
    <Route path="expenses" element={<Expenses />} />
    <Route path="invoices" element={<Invoices />} />
    <Route
      path="*"
      element={
        <main style={{ padding: "1rem" }}>
          <p>There's nothing here!</p>
        </main>
      }
    />
  </Route>

Index Routes

为这一层路由提供一个默认的 index 页面

<Routes>
  <Route path="/" element={<App />}>
    <Route path="invoices" element={<Invoices />}>
			/* Index */
      <Route
        index
        element={
          <main style={{ padding: "1rem" }}>
            <p>Select an invoice</p>
          </main>
        }
      />
    </Route>
</Routes>
  • Index 的作用

    • Index routes render in the parent routes outlet at the parent route's path.

    • Index routes match when a parent route matches but none of the other children match.

    • Index routes are the default child route for a parent route.

    • Index routes render when the user hasn't clicked one of the items in a navigation list yet

Params

  • URL Params (.../xxx)

    继续往下一层级添加一个 :key

    <Route path="invoices" element={<Invoices />}>
      <Route path=":invoiceId" element={<Invoice />} />
    </Route>

    在元素中使用 useParams() 来接住参入的参数

  • Search Params (...?key1=value1&key2=value2...)

    React Router makes it easy to read and manipulate the search params with useSearchParams. It works a lot like React.useState() but stores and sets the state in the URL search params instead of in memory.

    直接像 useState 一样声明就好了,调用 set… 时候,自动修改地址为 xxx=xxx

    获取 key 的 value 时也非常自然,直接读取变量就好

Use Location

useLocation returns a location that tells us information about the URL. A location looks something like this

{
  pathame: "/invoices",
  search: "?filter=sa",
  hash: "",
  state: null,
  key: "ae4cz2j"
}

Navigate

const navigate = useNavigate()

navigate('/xxx/xxx')
navigate(-1)	// 向后退一个

Last updated 3 years ago