只需要传递recommend的值
0.5=》表示半星
1=》表示整星
0=》表示空心
import React, { useMemo } from "react";
import emptyStar from "./image/empty-star.svg";
import halfStar from "./image/half-star.svg";
import fullStar from "./image/full-star.svg";
interface Props {
recommend: number;
}
export const Star: React.FC<Props> = ({ recommend }) => {
const stars = useMemo(() => {
let _stars: string[] = [];
for (let i = 0; i < 5; i++) {
_stars.push(
i + 1 <= recommend
? `${fullStar}`
: 0.5 + i <= recommend
? `${halfStar}`
: `${emptyStar}`
);
}
return _stars;
}, [recommend]);
return (
<div className="w-full h-10 bg-white flex justify-start items-center">
{stars.map((star, index) => {
return (
<span key={index} className="inline-block">
<img src={star} alt="" />
</span>
);
})}
</div>
);
};
例如:
<Star recommend={4} />
效果图:
例如:
<Star recommend={3.5} />
效果图