您能帮我将代码除以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>
</>
);
}
答案 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>
</>
);
}