简体   繁体   English

隐藏元素,如果它是 React 中的最后一项

[英]Hide Element If It the Last Item in React

I need to hide TimelineConnector if it is on the last item.如果它在最后一个项目上,我需要隐藏TimelineConnector How will i be able to hide it?我将如何隐藏它? Pls check my codesandbox请检查我的代码和框

CLICK HERE点击这里

 {timelines.lastIndexOf(index) !== 1 ? <TimelineConnector /> : ""}

只需与timelines长度进行比较:

{index !== timelines.length - 1 && <TimelineConnector />}

You could do using ternary operator您可以使用三元运算符

{timelines.map((timeline, index) =>
  index !== timelines.length - 1 ? (
    <TimelineItem>
       ...
    </TimelineItem>
  ) : null
)}

Forked demo分叉演示

编辑大力波-zi6xq

怎么样:

{timelines.length !== (index - 1) ? <TimelineConnector /> : null}
{index < (timelines.length - 1) && <TimelineConnector />}

Simply add a check of length with index - 1只需添加一个带有index - 1length检查index - 1

{index - 1 != timelines.length ? <TimelineConnector /> : ""}

Forked demo 分叉演示

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

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