Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Solution: Select Stars Based on Rating

My solution to Practicing State in React

Hi all,

First off, thanks to Laura for these updated React courses! They're very clear and provide a good understanding of the topics covered.

In this practice session, I thought I would share my solution to this challenge, as it was a little different to Laura's solution.

The git repo for my project files can be found here

The main differences are in the StarRating.js file.

Instead of rendering a seperate function to generate the renderStars component, I did this using a map directly in the JSX:

{[1, 2, 3, 4, 5].map((value) => (
    // ...
))}

I also added the same functionality to de-select a rating when it is clicked again whilst completing the code challenge. However, instead of using an if statement to reset the rating to 0, I used a ternary operator to toggle between the newRating and 0:

const updatedRating = newRating === rating ? 0 : newRating;
setRating(updatedRating);

Really enjoyed working on this one!

Kind regards, Berian