Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

react-rating

> 前端框架
Open source

Zero-dependency, highly customizable rating component for React

383 stars0 likes2 views
WebsiteGitHub

About

Zero-dependency, highly customizable rating component for React

React Rating

Zero dependency, highly customizable rating component for React.

Demo and Examples — NextJS Page Router — Vite

Features

  • Use any SVG: No headaches, icon fonts or packages to install in order to use your favorite shapes.
  • Endless possibilities of customization
  • Most common rating shapes included
  • Zero-config smart half-fill
  • Dead simple per-active-item styling
  • Built with accessibility in mind
  • Truly responsive and mobile-first
  • Controllable with React Hook Form
  • Simple DOM structure
  • Zero-config RTL support
  • Works with SSR

Installation

bash
pnpm add @smastrom/react-rating
bash
yarn add @smastrom/react-rating
bash
npm i @smastrom/react-rating

Usage

1. Import CSS and Rating component

javascript
import { Rating } from '@smastrom/react-rating'

import '@smastrom/react-rating/style.css'

Importing the CSS only once (most likely main.js or App.jsx) is enough to use Rating in any other component of your App.

Remix

app/root.tsx

typescript
import styles from '@smastrom/react-rating/style.css';

export function links() {
  return [{ rel: 'stylesheet', href: styles }];
}

export default function App() {
  // ...

in any page.tsx

typescript
import { Rating } from '@smastrom/react-rating';

export default function Index() {
  // ...

NextJS 13 - App Router

Interactive rating

app/layout.tsx

typescript
import '@smastrom/react-rating/style.css'

components/Rating.tsx

typescript
'use client'

import { useState } from 'react'
import { Rating as ReactRating } from '@smastrom/react-rating'

export function Rating() {
  const [rating, setRating] = useState(0)

  return 
}

in any page/component:

typescript
import { Rating } from './components/Rating'

export default function Home() {
  return (
    

  )
}

Non-interactive rating

app/layout.tsx

typescript
import '@smastrom/react-rating/style.css'

in any page/component:

typescript
import { Rating } from '@smastrom/react-rating'

export default function Home() {
  return (
    

  )
}

NextJS - Pages Router

pages/_app.js

javascript
import '@smastrom/react-rating/style.css';

function MyApp({ Component, pageProps }) {
  // ...

in any page/component:

typescript
import { Rating } from '@smastrom/react-rating';

export default function Home() {
  // ...

Gatsby

gatsby-browser.js - Create the file at the root of your project if it doesn't exist, and relaunch the dev server.

javascript
import '@smastrom/react-rating/style.css'

in any page/component:

typescript
import { Rating } from '@smastrom/react-rating';

const IndexPage = () => {
  // ...

Vite

main.jsx

javascript
import '@smastrom/react-rating/style.css'

in any component:

javascript
import { Rating } from '@smastrom/react-rating';

function App() {
  // ...

2. Give it a max-width and init the state

Since Rating will span across the entire container, define a maximum width via inline styles or css class:

javascript
function App() {
  const [rating, setRating] = useState(0) // Initial value

  return 
}

CSS Class

my-styles.css

css
.my-rating-class {
  max-width: 600px;
}

App.jsx

javascript
import './my-styles.css'

function App() {
  const [rating, setRating] = useState(0)

  return 
}

API

Legend

Color Description
:green_circle: Has always effect
:large_blue_circle: Has effect only if readOnly is false
:purple_circle: Has effect only if readOnly is true

:cyclone: Core

:thinking: Prop Description Type Default Required
:green_circle: value An integer from 0 to items. It can be a float if readOnly is true. number undefined :white_check_mark:
:large_blue_circle: onChange Setter or custom function to update the rating. RatingChange () => {} Only if readOnly is false
:large_blue_circle: onHoverChange Callback to execute while navigating the rating items. (hoveredValue: number) => void () => {} :x:
:green_circle: items Rating items to display. 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 5 :x:
:green_circle: readOnly Whether to render an accessible image element or not. boolean false :x:
:large_blue_circle: isDisabled Whether to disable the radio group or not. boolean false :x:
:large_blue_circle: isRequired Whether users should be able to set rating to 0 or not. boolean false :x:
:green_circle: preventDefault Whether or not to call event.preventDefault on click and Enter/Space select. click | keydown | all | none all :x:

ref, id, className, style, onBlur, onFocus are also available.

:nail_care: Appearance

:thinking: Prop Description Type Default Required
:green_circle: highlightOnlySelected Whether to highlight only the selected rating item or not. boolean false :x:
:purple_circle: halfFillMode Whether to half-fill the shape or the bounding box. svg | box svg :x:
:green_circle: itemStyles Custom shapes and colors. ItemStyles defaultStyles :x:
:green_circle: spaceInside Responsive padding of each rating item. none | small | medium | large small :x:
:green_circle: spaceBetween Responsive gap between rating items. none | small | medium | large none :x:
:green_circle: radius Responsive radius of the bounding box. none | small | medium | large | full none :x:
:green_circle: orientation Orientation of the rating items. horizontal | vertical horizontal :x:
:large_blue_circle: transition Transition to apply when hovering/selecting. none | zoom | colors | opacity | position colors :x:

:open_umbrella: Accessibility

:thinking: Prop Description Type Default Required
:green_circle: invisibleLabel Accessible label of the rating group / image. string • Rating Selection (radioGroup)
• Rated {value} on {items} (readOnly) :x:
:large_blue_circle: invisibleItemLabels Accessible labels of each rating item. string[] Rate 1, Rate 2... :x:
:large_blue_circle: visibleLabelId DOM ID of the element used as rating group label. If set, takes precedence over invisibleLabel. string undefined :x:
:large_blue_circle: visibleItemLabelIds DOM IDs of the elements used as labels for each rating item. If set, takes precedence over invisibleItemLabels. string[] undefined :x:
:large_blue_circle: resetLabel Accessible label of the reset radio button. string Reset Rating :x:

onChange

Type Definition

typescript
type RatingChange =
  | React.Dispatch>
  | ((selectedValue: number) => void | Promise)

Basic

If your app doesn't require any custom logic/state to set the rating, you can simply pass the setter to onChange:

javascript
function App() {
  const [rating, setRating] = useState(0)

  return 
}

Custom logic/state

If you need to perform actions while setting the rating (like calling an API) or you need to update a complex state, onChange accepts a callback whose only parameter equals to the selected rating:

javascript
function App() {
  const [state, setState] = useState({
    review: '',
    rating: 0 // Initial value
  })

  function handleChange(selectedValue) {
    // 1. Logs the selected rating (1, 2, 3...)
    console.log(selectedValue)

    // 2. Do something with or without the value...

    // 3. Update Rating UI
    setState((prevState) => ({
      ...prevState,
      rating: selectedValue
    }))
  }

  return 
}

Behavior

1. Rating with reset - Default

By default, the user is able to reset the rating (from 1-5 to 0 and vice versa):

Interaction Reset action Preview
Mouse By clicking again on the selected rating item
Keyboard By navigating to an invisible reset radio

:bulb: Don't like the default focus style? Check here how to customize it.

2. Rating without reset

There could be scenarios where you want to force the user to express a rating (e.g. review page, post-service rating).

In such cases, set isRequired to true:

javascript
  • It is not possible to reset by clicking again on the selected rating or by using the invisible radio.

  • It is

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptratingrating-starrating-starsreact

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架