React路由器V6链接到= {generateTostring}不工作

发布于 2025-02-10 09:14:33 字数 597 浏览 0 评论 0 原文

我正在将 react-router-dom 从v5升级到v6,我遇到了一个问题。

我有一个生成“到”字符串的函数:

const generateLinkTo = () => {
  switch (widget?.label) { // widget from a useState()
    case "Processed shipments":
      return "/shipments";
    case "Open quote requests":
      return "/quote-requests?display_status=open";
    default:
      return "/";
  }
};

,当我在 link 中使用它时

<Link to={generateLinkTo} className={`text-decoration-none ${textColor}`}>
  ...
</Link>

但是 在我从 React-Router-dom v5更新为v6之前。

I'm upgrading react-router-dom from v5 to v6 and I've encountered a problem.

I have a function which generates a 'to' string:

const generateLinkTo = () => {
  switch (widget?.label) { // widget from a useState()
    case "Processed shipments":
      return "/shipments";
    case "Open quote requests":
      return "/quote-requests?display_status=open";
    default:
      return "/";
  }
};

However, when I use this in the Link the <a> href is rendered as '/':

<Link to={generateLinkTo} className={`text-decoration-none ${textColor}`}>
  ...
</Link>

This worked before I updated from react-router-dom v5 to v6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守护在此方 2025-02-17 09:14:34

在React Router Router V6中,它不再接受 to prop中的功能。

declare function Link(props: LinkProps): React.ReactElement;

interface LinkProps
  extends Omit<
    React.AnchorHTMLAttributes<HTMLAnchorElement>,
    "href"
  > {
  replace?: boolean;
  state?: any;
  to: To;
  reloadDocument?: boolean;
}

type To = Partial<Location> | string;

https://reactrouter.com/docs/docs/docs/en/en/v6/components/components/components/link

export interface Location extends Path {
  /**
   * A value of arbitrary data associated with this location.
   *
   * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.state
   */
  state: unknown;

  /**
   * A unique string associated with this location. May be used to safely store
   * and retrieve data in some other storage API, like `localStorage`.
   *
   * Note: This value is always "default" on the initial location.
   *
   * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.key
   */
  key: Key;
}
/**
 * The pathname, search, and hash values of a URL.
 */
export interface Path {
  /**
   * A URL pathname, beginning with a /.
   *
   * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.pathname
   */
  pathname: Pathname;

  /**
   * A URL search string, beginning with a ?.
   *
   * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.search
   */
  search: Search;

  /**
   * A URL fragment identifier, beginning with a #.
   *
   * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.hash
   */
  hash: Hash;
}

In React Router v6 it no longer accepts a function in the To prop.

declare function Link(props: LinkProps): React.ReactElement;

interface LinkProps
  extends Omit<
    React.AnchorHTMLAttributes<HTMLAnchorElement>,
    "href"
  > {
  replace?: boolean;
  state?: any;
  to: To;
  reloadDocument?: boolean;
}

type To = Partial<Location> | string;

https://reactrouter.com/docs/en/v6/components/link

export interface Location extends Path {
  /**
   * A value of arbitrary data associated with this location.
   *
   * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.state
   */
  state: unknown;

  /**
   * A unique string associated with this location. May be used to safely store
   * and retrieve data in some other storage API, like `localStorage`.
   *
   * Note: This value is always "default" on the initial location.
   *
   * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.key
   */
  key: Key;
}
/**
 * The pathname, search, and hash values of a URL.
 */
export interface Path {
  /**
   * A URL pathname, beginning with a /.
   *
   * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.pathname
   */
  pathname: Pathname;

  /**
   * A URL search string, beginning with a ?.
   *
   * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.search
   */
  search: Search;

  /**
   * A URL fragment identifier, beginning with a #.
   *
   * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#location.hash
   */
  hash: Hash;
}

https://github.com/remix-run/history/blob/3e9dab413f4eda8d6bce565388c5ddb7aeff9f7e/packages/history/index.ts#L68

梦毁影碎の 2025-02-17 09:14:34

链接 react-Router-dom中的组件@6 仅采用字符串或部分 location>位置 object。

似乎只需要调用 generatelinkto 即可将链接的返回到 prop值。

const generateLinkTo = () => {
  switch (widget?.label) {
    case "Processed shipments":
      return "/shipments";
    case "Open quote requests":
      return "/quote-requests?display_status=open";
    default:
    return "/";
  }
};

...

<Link
  to={generateLinkTo()} // <-- invoke and use return value
  className={`text-decoration-none ${textColor}`}
>
  ...
</Link>

The Link component in react-router-dom@6 takes only a string or partial location object.

It seems the generateLinkTo just needs to be invoked to return the link's to prop value.

const generateLinkTo = () => {
  switch (widget?.label) {
    case "Processed shipments":
      return "/shipments";
    case "Open quote requests":
      return "/quote-requests?display_status=open";
    default:
    return "/";
  }
};

...

<Link
  to={generateLinkTo()} // <-- invoke and use return value
  className={`text-decoration-none ${textColor}`}
>
  ...
</Link>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文