useParams
declare function useParams<
K extends string = string
>(): Readonly<Params<K>>;
useParams
hook 會回傳一個物件,其中包含動態參數的 key/值配對,這些參數取自目前 URL,並且符合 <Route path>
。子路由會繼承其父路由的所有參數。
import * as React from 'react';
import { Routes, Route, useParams } from 'react-router-dom';
function ProfilePage() {
// Get the userId param from the URL.
let { userId } = useParams();
// ...
}
function App() {
return (
<Routes>
<Route path="users">
<Route path=":userId" element={<ProfilePage />} />
<Route path="me" element={...} />
</Route>
</Routes>
);
}