How You Can Build a Dynamic Star Rating Component in React.js (Full, Half, and Empty Stars Included)

When it comes to modern web applications, a star rating component is almost a must. Whether you’re working on a product page, a movie app, or a review system — users expect to see clear, beautiful ratings. Today, I'm walking you through how you can build a powerful and flexible Star Rating component in React.js — one that handles full, half, and empty stars perfectly. And don’t worry — it’s easier than you think. Let’s jump in! What We're Going to Build We'll create a React component that: Renders full, half, and empty stars dynamically. Always maintains a constant total number of stars (default: 5). Looks clean and polished using react-icons. Is reusable and customizable for different projects. Step 1: Install the Required Package First, we’ll need some star icons. Let’s grab them using react-icons: npm install react-icons Step 2: Understand the Star Logic Before we jump into the code, here’s the simple plan: Term Logic Total Stars A constant, usually 5. Full Stars Math.floor(rating) Half Star If rating is not an integer, add 1 half star. Empty Stars Remaining stars to complete 5. Important rule: There can only ever be at most one half star — no two half stars, no weird decimals! Step 3: Code the Star Rating Component Here’s the full working code: // StarRating.jsx import { FaStar, FaStarHalfAlt, FaRegStar } from "react-icons/fa"; const StarRating = ({ rating }) => { const totalStars = 5; // Full Stars const fullStars = Math.floor(rating); // Half Star const halfStars = Number.isInteger(rating) ? 0 : 1; // Empty Stars const emptyStars = totalStars - (fullStars + halfStars); // Build the stars array const starsArray = []; // Full stars for (let i = 0; i ); } // Half star if (halfStars) { starsArray.push(); } // Empty stars for (let i = 0; i ); } return ( {starsArray} ); }; export default StarRating; Step 4: How to Use It Super simple! Just import it and pass a rating prop: import StarRating from './StarRating'; function App() { return ( Ratings: ); } export default App; Here’s how it looks: Rating Visual Result 4.3 ⭐⭐⭐⭐

Apr 28, 2025 - 01:56
 0
How You Can Build a Dynamic Star Rating Component in React.js (Full, Half, and Empty Stars Included)

When it comes to modern web applications, a star rating component is almost a must.

Whether you’re working on a product page, a movie app, or a review system — users expect to see clear, beautiful ratings.

Today, I'm walking you through how you can build a powerful and flexible Star Rating component in React.js — one that handles full, half, and empty stars perfectly.

And don’t worry — it’s easier than you think. Let’s jump in!

What We're Going to Build

We'll create a React component that:

  • Renders full, half, and empty stars dynamically.
  • Always maintains a constant total number of stars (default: 5).
  • Looks clean and polished using react-icons.
  • Is reusable and customizable for different projects.

Step 1: Install the Required Package

First, we’ll need some star icons.

Let’s grab them using react-icons:

npm install react-icons

Step 2: Understand the Star Logic

Before we jump into the code, here’s the simple plan:

Term Logic
Total Stars A constant, usually 5.
Full Stars Math.floor(rating)
Half Star If rating is not an integer, add 1 half star.
Empty Stars Remaining stars to complete 5.

Important rule:

There can only ever be at most one half star — no two half stars, no weird decimals!

Step 3: Code the Star Rating Component

Here’s the full working code:

// StarRating.jsx
import { FaStar, FaStarHalfAlt, FaRegStar } from "react-icons/fa";

const StarRating = ({ rating }) => {
  const totalStars = 5;

  // Full Stars
  const fullStars = Math.floor(rating);

  // Half Star
  const halfStars = Number.isInteger(rating) ? 0 : 1;

  // Empty Stars
  const emptyStars = totalStars - (fullStars + halfStars);

  // Build the stars array
  const starsArray = [];

  // Full stars
  for (let i = 0; i < fullStars; i++) {
    starsArray.push(<FaStar key={`full-${i}`} color="gold" />);
  }

  // Half star
  if (halfStars) {
    starsArray.push(<FaStarHalfAlt key="half" color="gold" />);
  }

  // Empty stars
  for (let i = 0; i < emptyStars; i++) {
    starsArray.push(<FaRegStar key={`empty-${i}`} color="gray" />);
  }

  return (
    <div style={{ display: "flex", gap: "4px", alignItems: "center" }}>
      {starsArray}
    </div>
  );
};

export default StarRating;

Step 4: How to Use It

Super simple! Just import it and pass a rating prop:

import StarRating from './StarRating';

function App() {
  return (
    <div>
      <h1>Ratings:h1>
      <StarRating rating={4.3} />
      <StarRating rating={3} />
      <StarRating rating={2.7} />
    div>
  );
}

export default App;

Here’s how it looks:

Rating Visual Result
4.3 ⭐⭐⭐⭐

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies.