React Star Rate Component

2.13.1 · active · verified Sun Apr 19

rc-rate is a flexible and accessible React component for displaying and selecting star ratings. It is part of the extensive react-component ecosystem, known for providing foundational UI components. The package is currently at version 2.13.1 and actively maintained with frequent minor and patch releases, addressing bugs, accessibility, and adding features. Key differentiators include support for half-star selections, custom characters for stars (allowing SVG or any ReactNode), and robust keyboard navigation. It provides both controlled and uncontrolled modes of operation and ensures good accessibility practices, as evidenced by recent fixes. Developers can easily integrate it into projects that require user feedback or display ratings, with full TypeScript support provided. This component offers a solid, performant, and customizable base for rating functionalities in React applications, suitable for production use cases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic usage of `rc-rate`, including uncontrolled and controlled components, half-star selection, custom star characters, and disabled states. It also includes the essential CSS import.

import React, { useState } from 'react';
import ReactDOM from 'react-dom/client';
import Rate from 'rc-rate';
import 'rc-rate/assets/index.css'; // Don't forget to import the styles!

const App: React.FC = () => {
  const [value, setValue] = useState<number>(3.5);

  const handleChange = (newValue: number) => {
    console.log('New rating:', newValue);
    setValue(newValue);
  };

  return (
    <div>
      <h2>Basic Star Rating</h2>
      <Rate
        defaultValue={2.5}
        allowHalf
        onChange={(val) => console.log('Uncontrolled changed:', val)}
      />

      <h2>Controlled Star Rating (Value: {value})</h2>
      <Rate
        value={value}
        onChange={handleChange}
        allowHalf
        character={({ index }) => <span>{index + 1}</span>}
      />

      <h2>Disabled Rating</h2>
      <Rate value={4} disabled />

      <h2>Custom Icon Rating</h2>
      <Rate
        defaultValue={4}
        character={<span style={{ color: 'gold' }}>⭐</span>}
        count={7}
      />
    </div>
  );
};

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(<App />);

view raw JSON →