创建历史记录返回链接并利用我现有的链接组件

发布于 2025-01-28 19:24:46 字数 1310 浏览 3 评论 0 原文

我有一定的通用链接组件(一个原子,我正在使用原子设计来构建我的react应用程序),它要么只返回带有标题和箭头图标的< span> url 它返回一个react-router-dom < link>

我正在使用React-Router-Dom V5。

import React from 'react';

import { Link } from 'react-router-dom';

import { Arrow} from 'app/icons';

import { Props } from './MyLink.types';

const MyLink = ({ variant = 'iconRight', text, url }: Props) => {
  const renderLinkBody = () => {
    return (
      <React.Fragment>
        <span css={styles.label}>{text}</span>
        <Arrow />
      </React.Fragment>
    );
  };

  return url !== undefined ? (
    <Link to={url} title={text} >
      {renderLinkBody()}
    </Link>
  ) : (
    <span>{renderLinkBody()}</span>
  );
};

export default MyLink;

现在,我想使用此组件来创建一个返回按钮(类似于浏览器返回按钮的行为)。

这是一种干净的方法,将OnClick Prop(功能)从父母到孩子,

const history = useHistory();

<MyLink text="My title" variant="iconLeft" onClick={history.goBack} />

然后将MyLink组件调整一点:

 return url !== undefined || onClick ? (
    <Link to={url} title={text} onClick={onClick} >
      {renderLinkBody()}
    </Link>
  ) : (
    <span>{renderLinkBody()}</span>
  );

I have a certain generic link component (an atom, I am using Atomic design for structuring my React app), which either returns just a <span> with a Title and arrow icon or when it does have a url it returns a react-router-dom <Link>.

I am using react-router-dom v5.

import React from 'react';

import { Link } from 'react-router-dom';

import { Arrow} from 'app/icons';

import { Props } from './MyLink.types';

const MyLink = ({ variant = 'iconRight', text, url }: Props) => {
  const renderLinkBody = () => {
    return (
      <React.Fragment>
        <span css={styles.label}>{text}</span>
        <Arrow />
      </React.Fragment>
    );
  };

  return url !== undefined ? (
    <Link to={url} title={text} >
      {renderLinkBody()}
    </Link>
  ) : (
    <span>{renderLinkBody()}</span>
  );
};

export default MyLink;

Now I want to use this component for creating a Back button (similar behaviour as the browser back button).

Is it a clean approach passing an onClick prop (function) from parent to child, something like:

const history = useHistory();

<MyLink text="My title" variant="iconLeft" onClick={history.goBack} />

and then adjust MyLink component a bit:

 return url !== undefined || onClick ? (
    <Link to={url} title={text} onClick={onClick} >
      {renderLinkBody()}
    </Link>
  ) : (
    <span>{renderLinkBody()}</span>
  );

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

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

发布评论

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

评论(1

柳絮泡泡 2025-02-04 19:24:46

我认为这种方法还不错。将 onclick 处理程序附加到链接组件时,您可能需要防止默认链接行为发生。使用单击处理程序时,您还需要将有效的 prop提供给链接组件。

示例:

const MyLink = ({ variant = "iconRight", onClick, text, url }) => {
  const clickHandler = e => {
    if (onClick) {
      e.preventDefault();
      onClick();
    }
  };

  const renderLinkBody = () => {
    return (
      <>
        <span css={styles.label}>{text}</span>
        <Arrow />
      </>
    );
  };

  return url !== undefined || onClick ? (
    <Link to={url || "/"} title={text} onClick={clickHandler} >
      {renderLinkBody()}
    </Link>
  ) : (
    <span>{renderLinkBody()}</span>
  );
};

由于 onclick link 操作的问题,并且重构 myLink 根据需求有条件地渲染按钮,链接或跨度。

I think this approach isn't bad. When attaching an onClick handler to the Link component you will likely need to prevent the default link behavior from occurring. You will also need to provide a valid to prop to the Link component when using a click handler.

Example:

const MyLink = ({ variant = "iconRight", onClick, text, url }) => {
  const clickHandler = e => {
    if (onClick) {
      e.preventDefault();
      onClick();
    }
  };

  const renderLinkBody = () => {
    return (
      <>
        <span css={styles.label}>{text}</span>
        <Arrow />
      </>
    );
  };

  return url !== undefined || onClick ? (
    <Link to={url || "/"} title={text} onClick={clickHandler} >
      {renderLinkBody()}
    </Link>
  ) : (
    <span>{renderLinkBody()}</span>
  );
};

Edit react-creating-a-history-back-link-and-make-use-of-my-existing-link-component

Because of the issue with the onClick and the Link action, you may want to make the "back" navigation a button instead, and refactor MyLink to conditionally render the button, the link, or the span, depending on needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文