Skip to content

Conversation

ashikshafi08
Copy link

@ashikshafi08 ashikshafi08 commented Mar 30, 2023

Much more dynamic solution for generating the stars.

import React from "react";

export default function Rating({ value, text, color }) {
  const stars = [];
  for (let i = 0; i < 5; i++) {
    if (value >= i + 1) {
      stars.push(<i key={i} style={{ color }} className="fas fa-star"></i>);
    } else if (value >= i + 0.5) {
      stars.push(
        <i
          key={i}
          style={{ color }}
          className="fas fa-star-half-alt"
        ></i>
      );
    } else {
      stars.push(<i key={i} style={{ color }} className="far fa-star"></i>);
    }
  }

  return (
    <div className="rating">
      {stars}
      <span style={{ paddingLeft: "10px" }}>{text && text}</span>
    </div>
  );
}

Much more dynamic solution.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant