useHref

useHref

根据当前位置解析完整的 URL,以用作 hreflinkhref。如果提供了相对路径,它将解析为完整的 URL。

¥Resolves a full URL against the current location to be used as an href to a link. If a relative path is supplied, it will resolve to a full URL.

import { useHref } from "@remix-run/react";

function SomeComponent() {
  const href = useHref("some/where");

  return <a href={href}>Link</a>;
}

签名

¥Signature

useHref(to, options)

to

可选。附加到已解析 URL 的路径。

¥Optional. The path to append to the resolved URL.

请参阅 useResolvedPath 文档中的 Splat 路径 部分,了解 future.v3_relativeSplatPath 未来标志在 splat 路由中相对于 useHref() 行为的行为

options

唯一的选项是 { relative: "route" | "path"},它定义了解析相对 URL 时的行为。

¥The only option is { relative: "route" | "path"}, which defines the behavior when resolving relative URLs.

  • route default — 相对于路由层级,而非 URL

    ¥route default — relative to the route hierarchy, not the URL

  • path — 使操作相对于 URL 路径,因此 .. 将删除一个 URL 段。

    ¥path — makes the action relative to the URL paths, so .. will remove one URL segment.

Remix v2.17 中文网 - 粤ICP备13048890号