useResolvedPath
本页内容

useResolvedPath

摘要

参考文档 ↗

根据当前的 Location 解析给定的 to 值的路径名。类似于 useHref,但返回一个 Path 对象而不是字符串。

import { useResolvedPath } from "react-router";

function SomeComponent() {
  // if the user is at /dashboard/profile
  let path = useResolvedPath("../accounts");
  path.pathname; // "/dashboard/accounts"
  path.search; // ""
  path.hash; // ""
}

签名

function useResolvedPath(
  to: To,
  {
    relative,
  }: {
    relative?: RelativeRoutingType;
  } = ,
): Path {}

参数

to

要解析的路径

options.relative

默认为 "route",因此路由是相对于路由树的。设置为 "path" 则使相对路由操作相对于路径段。

返回

已解析的 Path 对象,包含 pathnamesearchhash

文档和示例 CC 4.0
编辑