英文:
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').
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 = ["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').
I tried applying _last={{ display: "none" }} 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 '@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>
  )
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论