如何区分`.map`中的最后一个元素的样式。

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

How do I differentiate style for last element in .map

问题

First off, let me leave the codesandbox for this problem here.

I am mapping through an array of letters.

const letters = ["A", "B", "C", "D", "E", "F"];

The idea is for all of the letters except the last one to have an arrow below it pointing to the next letter. How can I accomplish this?

const App = () => {
  return (
    <Box>
      {letters.map((letter, index) => (
        <Letter key={index} letter={letter} />
      ))}
    </Box>
  );
};
const Letter = ({ letter }) => {
  return (
    <Box
      display="flex"
      flexDirection="column"
      justifyContent="center"
      alignItems="center"
    >
      <Text>{letter}</Text>
      <ArrowDownIcon />
    </Box>
  );
};

Currently, I am getting what's pictured below. I need to figure out how to remove the last arrow (the one below 'F').

如何区分`.map`中的最后一个元素的样式。

I tried applying _last={{ display: "none" }} to the ArrowDownIcon but this just removed all of the arrows.

英文:

First off, let me leave the codesandbox for this problem here.

I am mapping through an array of letters.

const letters = [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;];

The idea is for all of the letters except the last one to have an arrow below it pointing to the next letter. How can I accomplish this?

const App = () =&gt; {
  return (
    &lt;Box&gt;
      {letters.map((letter, index) =&gt; (
        &lt;Letter key={index} letter={letter} /&gt;
      ))}
    &lt;/Box&gt;
  );
};
const Letter = ({ letter }) =&gt; {
  return (
    &lt;Box
      display=&quot;flex&quot;
      flexDirection=&quot;column&quot;
      justifyContent=&quot;center&quot;
      alignItems=&quot;center&quot;
    &gt;
      &lt;Text&gt;{letter}&lt;/Text&gt;
      &lt;ArrowDownIcon /&gt;
    &lt;/Box&gt;
  );
};

Currently, I am getting what's pictured below. I need to figure out how to remove the last arrow (the one below 'F').

如何区分`.map`中的最后一个元素的样式。

I tried applying _last={{ display: &quot;none&quot; }} to the ArrowDownIcon but this just removed all of the arrows.

答案1

得分: 2

你可以使用CSS伪类 :last-child

英文:

You could take CSS pseudo class :last-child.

答案2

得分: 1

你可以将一个属性传递给 Letter 组件,该属性检查每个元素是否为最后一个,然后根据相同的属性有条件地渲染图标。

import { Box, Text } from '@chakra-ui/react';
import { ArrowDownIcon } from '@chakra-ui/icons';

const Letter = ({ letter, isLast }) => {
  return (
    <Box
      display="flex"
      flexDirection="column"
      justifyContent="center"
      alignItems="center"
    >
      <Text>{letter}</Text>
      {!isLast && <ArrowDownIcon />}
    </Box>
  );
}

export const App = () => {
  const letters = ['A', 'B', 'C', 'D', 'E', 'F'];
  return (
    <Box mt={10}>
      {letters.map((letter, index) => (
        <Letter
          key={index}
          isLast={index + 1 === letters.length}
          letter={letter}
        />
      ))}
    </Box>
  );
}
英文:

You can send a prop to the Letter component which checks whether each element is the last or not and render the icon conditionally using the same prop.

import { Box, Text } from &#39;@chakra-ui/react&#39;
import { ArrowDownIcon } from &#39;@chakra-ui/icons&#39;

const Letter = ({ letter, isLast }) =&gt; {
  return (
    &lt;Box
      display=&quot;flex&quot;
      flexDirection=&quot;column&quot;
      justifyContent=&quot;center&quot;
      alignItems=&quot;center&quot;
    &gt;
      &lt;Text&gt;{letter}&lt;/Text&gt;
      {!isLast &amp;&amp; &lt;ArrowDownIcon /&gt;}
    &lt;/Box&gt;
  )
}

export const App = () =&gt; {
  const letters = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;]
  return (
    &lt;Box mt={10}&gt;
      {letters.map((letter, index) =&gt; (
        &lt;Letter
          key={index}
          isLast={index + 1 === letters.length}
          letter={letter}
        /&gt;
      ))}
    &lt;/Box&gt;
  )
}

huangapple
  • 本文由 发表于 2023年3月7日 21:21:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662507.html
匿名

发表评论

匿名网友

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

确定