Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<Rating /> component to be fully controlled #59

Open
kevingjs opened this issue Oct 16, 2024 · 0 comments
Open

<Rating /> component to be fully controlled #59

kevingjs opened this issue Oct 16, 2024 · 0 comments

Comments

@kevingjs
Copy link

How should I set the <Rating /> component to be completely controlled? That is, the value depends completely on some state.

Because even if I don't specify the onClick and initialValue attributes, <Rating /> will set the star value on click. I need the <Rating /> component to be completely dependent on some state like const [ rate, setRate ] = useState(0).

Example:

This way the component still sets the stars that are filled when clicked, even if the rate state didn't change.

import { useState } from 'react';
import { Rating } from 'react-simple-star-rating';

const App = () => {
    const [ rate, setRate ] = useState(0);
    const isLogged = false;
    
    const handleRate = value => {
        if (!isLogged) return; // here I'm trying to prevent stars to be filled if isLogged is false
        setRate(value);
    };
    
    return (
        <Rating
            initialValue={ rate }
            onClick={ handleRate }
            iconsCount={ 5 }
            allowFraction
        />
    );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant