Skip to main content

Command Palette

Search for a command to run...

How to implement a star rating component in ReactJS

Published
2 min read
How to implement a star rating component in ReactJS
F
I am an Infrastructure and DevOps Engineer specializing in designing, building, and operating scalable, secure, and highly available cloud infrastructure. My core focus is on Microsoft Azure cloud platforms, Infrastructure as Code (IaC), and DevOps automation to support reliable production systems. I work across cloud infrastructure engineering, DevOps practices, and site reliability engineering (SRE) principles to ensure systems are resilient, observable, and optimized for performance, cost, and scalability. My experience includes designing and managing cloud environments across compute, networking, storage, identity, and security layers. I build Infrastructure as Code solutions using Terraform and Azure Resource Manager (ARM) templates to automate provisioning, configuration, and deployment of cloud resources. I am actively involved in improving system reliability through monitoring, logging, and incident response processes using tools such as Azure Monitor and cloud-native observability solutions. I also participate in on-call operations, production support, and incident management to ensure high availability of critical systems. Security is a core part of my engineering approach. I work with identity and access management (IAM), Azure Active Directory, and cloud security best practices to ensure infrastructure remains compliant, secure, and audit-ready in line with industry standards such as ISO 9001 and ISO 27001. I collaborate with cross-functional teams including software engineers, DevSecOps, and product teams to deliver infrastructure solutions for customer-facing applications and enterprise platforms. My technical interests and growth areas include: Cloud Infrastructure Engineering (Azure, AWS, GCP) Site Reliability Engineering (SRE) Platform Engineering Kubernetes & Container Orchestration Infrastructure as Code (Terraform, ARM) CI/CD Pipeline Automation Distributed Systems & System Design Cloud Security & Identity Management I am passionate about building systems that are not only scalable and efficient but also reliable and easy for engineers to use. I am continuously growing my expertise toward senior-level Infrastructure, SRE, and Platform Engineering roles, including global remote opportunities.

Define a component, called, Star that will render each individual star with the appropriate appearance, based on the parent component's state. In the StarRating component, use the React.setState() hook to define the rating and selection state variables with the initial values of props.rating (or 0 if invalid or not supplied) and 0. Create a method, hoverOver, that updates selected and rating according to the provided event. Create a <div> to wrap the <Star> components, which are created using Array.prototype.map on an array of 5 elements, created using Array.from, and handle the onMouseLeave event to set selection to 0, the onClick event to set the rating and the onMouseOver event to set selection to the star-id attribute of the event.target respectively. Finally, pass the appropriate values to each <Star> component (starId and marked).

Here are some code samples:

Star.js

import React from "react";
import "./App.css";

function Star({ marked, starId }) {
  return (
    <div className="App">
      <span
        className="starStyling"
        star-id={starId}
        style={{ color: "#ff9933" }}
        role="button"
      >
        {marked ? "\u2605" : "\u2606"}
      </span>
    </div>
  );
}

function StarRating(props) {
  const [rating, setRating] = React.useState(
    typeof props.rating == "number" ? props.rating : 0
  );
  const [selection, setSelection] = React.useState(0);
  const hoverOver = (event) => {
    let val = 0;
    if (event && event.target && event.target.getAttribute("star-id"))
      val = event.target.getAttribute("star-id");
    setSelection(val);
  };
  return (
    <div
      onMouseOut={() => hoverOver(null)}
      onClick={(event) =>
        setRating(event.target.getAttribute("star-id") || this.state.rating)
      }
      onMouseOver={hoverOver}
      className=""
    >
      {Array.from({ length: 5 }, (v, i) => (
        <Star
          starId={i + 1}
          key={`star_${i + 1}`}
          marked={selection ? selection >= i + 1 : rating >= i + 1}
        />
      ))}
    </div>
  );
}

export default StarRating;

index.js

import React from "react";
import ReactDOM from "react-dom/client";
import StarRating from "./Star";
import "./index.css";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <div className="indexStyle">
    <StarRating rating={2} />
  </div>
);

Star.css

div{
  display: flex;
}

index.css

.indexStyle{
  display: block;
margin-top: 20%;
  margin-left: auto;
    margin-right: auto;
  background-color: #311515;
}

Here is the screenshot of the output:

starrating.png

Source Code

Thanks for reading...

Happy Coding!

More from this blog

Building Reliable Systems

93 posts

Insights on Infrastructure, DevOps, SRE, and building reliable systems at scale.