简体   繁体   English

在React中有条件地在div周围渲染锚链接

[英]Conditionally render anchor link around div in React

I have a component which is wrapped in an anchor link and has hover styling. 我有一个包裹在锚链接中并具有悬停样式的组件。 I need to be able to conditionally render the channel.url link on the div if there is an available url in the data for the particular element that has been looped through. 如果在数据中存在已循环通过的特定元素的可用URL,则我需要能够有条件地在div上呈现channel.url链接。

I tried checking if the length of the channel.url is greater than 0, but my logic wasn't working. 我尝试检查channel.url的长度是否大于0,但是我的逻辑不起作用。 In the end, it should simply be that if the particular item that's looped through doesn't have a url in its data object, it will simply not have the anchor link applied to it. 最后,应该简单地是,如果循环通过的特定项在其数据对象中没有url,则根本不会对其应用锚链接。

  <ul>
    {items.children.map((channel, i) => (
      (channel, i) =>
            channel.url ? (
              <a href={channel.url} target="_blank">
                <FeaturedApps />
              </a>
            ) : (
              <FeaturedApps />
            )
        )}
    ))}
  </ul>

UPDATE 更新

I got the logic working, thanks to Ethan. 感谢Ethan,我的逻辑得以解决。 Trying to clean it up thought and move the li to its own component but having an issue passing icon in. 尝试清理思路并将li移至其自己的组件,但在传递icon时出现问题。

COMPONENT 零件

const FeaturedApps = ({ iconName, className }) => {
  <li>
    <IconForChannel iconName={channel.icon} className={classes.icon} />
    <span className={classes.channelName}>
      {channel.name}{" "}
      <Icon className={classes.nameIcon} name={channel.nameicon} size={12} />
    </span>
  </li>;
};

LOOP WITH LOGIC 有逻辑的环

 <ul>
    {items.children.map((channel, i) => (
      (channel, i) =>
            channel.url ? (
              <a href={channel.url} target="_blank">
                <FeaturedApps />
              </a>
            ) : (
              <FeaturedApps />
            )
        )}
    ))}
  </ul>

You could save the 'inner' part to a variable and render conditionally, ie 您可以将“内部”部分保存到变量中并有条件地进行渲染,即

<ul>
  {items.children.map((channel, i) => {
    let inner = (<span className={classes.channelName}>
      {channel.name}
    </span>);
    return (
      <li key={i}>
        { (channel.url ? (<a href={channel.url} target="_blank">{inner}</a>) : inner) }
      </li>
    </a>
    )};
  )}
</ul>

it was mentioned above that li should always be the direct child of ul, and I've adjusted that also while at it. 上面提到过,li应该始终是ul的直系子孙,而我在调整时也已对此进行了调整。 Hope this is helpful. 希望这会有所帮助。

This may work: 这可能起作用:

  <ul>
    {items.children.map((channel, i) => (
      (channel.url) ?
      <a href={channel.url} target="_blank">
        <li key={i}>
          <span className={classes.channelName}>
            {channel.name}
          </span>
        </li>
      </a>
      :
      <li key={i}>
        <span className={classes.channelName}>
          {channel.name}
        </span>
      </li>
    ))}
  </ul>

This ternary operator is saying: "for each channel, if that channel has a url as a property, render that list item wrapped in the channel.url as an anchor link, else, simply render the list item." 该三元运算符说:“对于每个通道,如果该通道具有url作为属性,则将包裹在channel.url中的列表项呈现为锚链接,否则,只需呈现该列表项。”

condition ? 条件? expr1 : expr2 expr1:expr2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM