React Star Rate Component
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
-
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
cause Incorrect default export handling for the `Rate` component when using CommonJS `require` in an environment that expects `.default`.fixIf using `require`, modify your import statement to `const Rate = require('rc-rate').default;` to correctly access the default export. -
Module not found: Error: Can't resolve 'rc-rate/assets/index.css'
cause The stylesheet for `rc-rate` is missing or the import path specified is incorrect, or your build system is not configured to handle CSS imports.fixEnsure `import 'rc-rate/assets/index.css';` is present in your application code and that your build tool (e.g., Webpack, Vite) is correctly configured to process CSS files. -
TypeError: Cannot read properties of undefined (reading 'Provider') or Invariant Violation: Objects are not valid as a React child
cause This usually indicates a mismatch in React versions or multiple instances of React being loaded, particularly if `rc-rate`'s peer dependency requirements are not met.fixVerify that `react` and `react-dom` versions `>=16.9.0` are installed and resolve correctly in your project. Ensure only one instance of React is loaded, especially in monorepos or complex application setups.
Warnings
- breaking There is a new package, `@rc-component/rate`, which appears to be a direct successor or rename of `rc-rate`. While `rc-rate` is still active at `v2.13.1`, future major development might shift to the `@rc-component/rate` namespace. Developers should monitor the project's official channels for guidance on migration or future support.
- gotcha The component's default styling is not bundled with the component logic and must be imported separately. Forgetting to import the CSS will result in an unstyled, potentially unusable component.
- gotcha Versions prior to `2.13.1` had an incorrect ARIA role for accessibility (`role` attribute), potentially impacting users relying on screen readers for navigation and understanding.
- gotcha Prior to `v2.13.0`, keyboard navigation for the rating component could not be explicitly disabled, which might interfere with specific interaction patterns or accessibility requirements.
Install
-
npm install rc-rate -
yarn add rc-rate -
pnpm add rc-rate
Imports
- Rate
import { Rate } from 'rc-rate';import Rate from 'rc-rate';
- RateProps
import type { RateProps } from 'rc-rate'; - Rate (CommonJS)
const Rate = require('rc-rate');const Rate = require('rc-rate').default;
Quickstart
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 />);