For AI agents: the complete documentation index is available at https://rspress.rs/zh/llms.txt, the full documentation bundle is available at https://rspress.rs/zh/llms-full.txt, and this page is available as Markdown at https://rspress.rs/zh/ui/hooks/router-hooks.md.
close

路由 Hooks

Rspress 直接重导出 react-router-dom 的路由能力,你可以无需额外依赖就访问导航与位置信息。常用的 Hook 包括 useLocation、useNavigate、useParams、useSearchParams 和 useMatches。

  • 类型:与对应的 react-router-dom Hook 保持一致

自 Rspress 2.1.0 起,路由使用 React Router 的 Data 模式。

下面是一个使用示例:

当前路径:/zh/ui/hooks/router-hooks
import { useLocation, useNavigate } from '@rspress/core/runtime';

export default function LocationDebugger() {
  const location = useLocation();
  const navigate = useNavigate();

  return (
    <div>
      <div>当前路径:{location.pathname}</div>
      <button type="button" onClick={() => navigate('/')}>
        返回首页
      </button>
    </div>
  );
}