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 包括 useLocationuseNavigateuseParamsuseSearchParamsuseMatches

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

    下面是一个使用示例:

    当前路径:/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>
      );
    }