您能帮我转换此React星级吗?

时间:2020-09-27 06:39:06

标签: reactjs

您能帮我将代码除以2,然后将代码转换为5星级吗? 如果星星的索引为

export default function StarRating({ totalStars = 5 }) {
    const [selectedStars, setSelectedStars] = useState(0);
    return (
       <>
       {createArray(totalStars).map((n, i) => (
       <Star
          key={i}
          selected={selectedStars > i}
          onSelect={() => setSelectedStars(i + 1)}
       />
    ))}
      <p>
       {selectedStars} of {totalStars} stars
      </p>
      </>
  );
}

1 个答案:

答案 0 :(得分:0)

由于我看不到create array做什么,它会做这样的事情吗?如果没有,这可能是您的解决方案

  export default function StarRating({ totalStars = 5 }) {
    const [selectedStars, setSelectedStars] = useState(0);
    var totalStarsArray = [];
    for(var j = 1;j <= totalStars;j++)
    {
        totalStarsArray.push(j);
    }
    return (
       <>
       {totalStarsArray.map((n, i) => (
       <Star
          key={n}
          selected={selectedStars > n}
          onSelect={() => setSelectedStars(n+ 1)}
       />
    ))}
      <p>
       {selectedStars} of {totalStars} stars
      </p>
      </>
  );
}