Passing a parameter to onClick function NextJS

Viewed 48

I'm currently using the package react-simple-star-rating with this example. But here's my problem :

My trains parameter contain a variable id I'd like to pass to the handleRating function using onClick but I can't figure it how.

const handleRating = (rate) => {
    console.log(rate)
}

const trainingLine = (trains) => (
    <Rating onClick={handleRating} ratingValue={Math.round(trains.rate.reduce((partialSum, a) => partialSum + a, 0)/trains.rate.length)}/>
)

Do you have an idea to achieve that ?

2 Answers

You can rewrite your handleRating function as

    const handleRating = useCallback(
    (raiting: any) => (e: React.MouseEvent<HTMLElement>) => {
        // do something
    },
    [...deps]);

Okay I found the solution well I think I missed something but in order to pass rating and trains.id variables to the handleRating function, I just need to to do this :

const handleRating = (rating, id) => {
    // Do something
}

<Rating onClick={(rating) => handleRating(rating, trains._id)} ratingValue={Math.round(trains.rate.reduce((partialSum, a) => partialSum + a, 0)/trains.rate.length)}/>

Lot of struggle for something pretty understandable, I need some more practice ! Thank you guys for your answers.

Related