stars-rating-react-hooks
TypeScript icon, indicating that this package has built-in type declarations

0.2.4 • Public • Published

stars-rating-react-hooks ⭐️

stars-rating-react-hooks

React Rating Stars - Customizable and headless hooks.

See Website and demos


NPM

Install

npm install --save stars-rating-react-hooks

Usage (Basic)

stars-rating-react-hooks

import React from 'react'

import { StarsRating } from "stars-rating-react-hooks";

function Example() {

  const config = {
    totalStars: 5,
    initialSelectedValue: 4.5,
    renderFull: (
      <img src="https://img.icons8.com/ios-filled/50/000000/star--v1.png" />
    ),
    renderEmpty: (
      <img src="https://img.icons8.com/ios/50/000000/star--v1.png" />
    ),
    renderHalf: (
      <img src="https://img.icons8.com/ios-filled/50/000000/star-half-empty.png" />
    )
  };



    return  <StarsRating config={config} />

}

export default Example

Usage (Customizable and Headless)

import { useStars } from 'stars-rating-react-hooks'

const config = {
    totalStars: 5,
    initialSelectedValue: 2,
    renderFull: '★',
    renderEmpty: '☆',
  };

const {
    stars,
    getStarProps,
    getStarWrapperProps,
    isSelecting,
    selectingValue,
    selectedValue,
  } = useStars(config);

             <span
              {...getStarWrapperProps({
                style: {
                  cursor: 'pointer',
                  display: 'inline-block'
                },
              })}
            >
              {stars?.map((star, i) => (
                <span
                  key={i}
                  {...getStarProps(i, {
                    style: {
                      fontSize: '40px',
                      display: 'inline-block'
                    },
                    onClick: (event, ratedValue) => {
                      console.log(`You just rated ${ratedValue} Stars!!`);
                    },
                  })}
                >
                  {star}
                </span>
              ))}
            </span>

'getStarWrapperProps' and 'getStarProps' are prop getters, More info on prop getters

Demo:

Basic

Open Basic codesandbox

Customizable and Headless:

Open Customizable codesandbox

License

MIT © 07harish

Package Sidebar

Install

npm i stars-rating-react-hooks

Weekly Downloads

24

Version

0.2.4

License

MIT

Unpacked Size

78.4 kB

Total Files

20

Last publish

Collaborators

  • 07harish