React从li内的锚点中删除最后一个URL。

huangapple go评论81阅读模式
英文:

React remove last url from anchor inside li

问题

I want to remove the last url of an anchor tag (href="") inside a list in React.

This is the code that I have:

const Link = ({ ...props }) => <TranslationsLink {...props} />;

const Navbar: FC<Props> = () => {
  return (
    <Nav>
      <List>
        {list.map((el, i) => {
          const lastEl = i === list.length - 1;

          return (
            <Item key={el.route}>
              <Link
                lastEl={lastEl}
              >
                {el.text}
              </Link>
            </Item>
          );
        })}
      </List>
    </Nav>
  );
};

const TranslationsLink: FC<ITranslationsLink> = memo(({ ...props }) => {
  const { findRouteByName, activeLocale } = useRouter();

  const routeMatch = findRouteByName(route, locale || activeLocale);

  const url = routeMatch.getUrl(params);
  const nextLink = routeMatch.getNextLink(params);

  const link = (
    <a href={url} {...props}>
      {children}
    </a>
  );

  return <Link href={nextLink} as={url} children={link} />;
});

I tried passing the lastEl to the Link component so I can override its href:

<Link href={props.lastEl ? '' : nextLink}>

The problem is that because that same link it's being used in other places I'm getting false... How can I avoid that without creating another Link component for the last element?

英文:

I want to remove the last url of an anchor tag (href="") inside a list in React.

This is the code that I have:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const Link = ({ ...props }) =&gt; &lt;TranslationsLink {...props} /&gt;;

const Navbar: FC&lt;Props&gt; = () =&gt; {
  return (
    &lt;Nav&gt;
      &lt;List&gt;
        {list.map((el, i) =&gt; {
          const lastEl = i === list.length - 1;

          return (
            &lt;Item key={el.route}&gt;
              &lt;Link
                lastEl={lastEl}
              &gt;
                {el.text}
              &lt;/Link&gt;
            &lt;/Item&gt;
          );
        })}
      &lt;/List&gt;
    &lt;/Nav&gt;
  );
};

const TranslationsLink: FC&lt;ITranslationsLink&gt; = memo(({ ...props }) =&gt; {
  const { findRouteByName, activeLocale } = useRouter();

  const routeMatch = findRouteByName(route, locale || activeLocale);

  const url = routeMatch.getUrl(params);
  const nextLink = routeMatch.getNextLink(params);

  const link = (
    &lt;a href={url} {...props}&gt;
      {children}
    &lt;/a&gt;
  );

  return &lt;Link href={nextLink} as={url} children={link} /&gt;;
});

<!-- end snippet -->

I tried passing the lastEl to the Link component so I can override it's href:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;Link href={props.lastEl ? &#39;&#39; : nextLink}

<!-- end snippet -->

The problem is that because that same link it's being used in other places I'm getting false... How can I avoid that without creating another Link component for the last element?

答案1

得分: 1

你可以像这样给你的地图添加条件:

const Navbar: FC<Props> = () => {
  return (
    <Nav>
      <List>
        {list.map((el, i) => {
          const lastEl = i === list.length - 1;

          return lastEl ? null : // 在这里检查最后一个元素
            (<Item key={el.route}>
              <Link
                lastEl={lastEl}
              >
                {el.text}
              </Link>
            </Item>
          );
        })}
      </List>
    </Nav>
  );
};

请注意,这只是代码的一部分,所以我只翻译了这部分内容。

英文:

You can add condition to your map like this:

const Navbar: FC&lt;Props&gt; = () =&gt; {
  return (
    &lt;Nav&gt;
      &lt;List&gt;
        {list.map((el, i) =&gt; {
          const lastEl = i === list.length - 1;

          return lastEl ? null : // just check for the lastEl here
            (&lt;Item key={el.route}&gt;
              &lt;Link
                lastEl={lastEl}
              &gt;
                {el.text}
              &lt;/Link&gt;
            &lt;/Item&gt;
          );
        })}
      &lt;/List&gt;
    &lt;/Nav&gt;
  );
};

答案2

得分: 0

我找到了答案:

const Navbar: FC<Props> = () => {
  return (
    <Nav>
      <List>
        {list.map((el, i) => {
          const lastEl = i === list.length - 1;

          return (
            <Item key={el.route}>
              {lastEl ? (
                <span>{el.text}</span>
              ) : (
                <Link>{el.text}</Link>
              )}
            </Item>
          );
        })}
      </List>
    </Nav>
  );
};
英文:

I found the answer:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const Navbar: FC&lt;Props&gt; = () =&gt; {
  return (
    &lt;Nav&gt;
      &lt;List&gt;
        {list.map((el, i) =&gt; {
          const lastEl = i === list.length - 1;

          return
            (&lt;Item key={el.route}&gt;
            {lastEl ? (
                &lt;span&gt;{el.text}&lt;/span&gt;
              ) : (
                &lt;Link&gt;
                  {el.text}
                &lt;/Link&gt;
              )}
            &lt;/Item&gt;
          );
        })}
      &lt;/List&gt;
    &lt;/Nav&gt;
  );
};

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 17:55:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定