Jump to content

React Star Ratings

I'm implementing a pop up window on a webpage where a user can leave a review on a web app I'm working on using React. Right now, I have a Modal that opens and the user is presented with a Star Rating component they can select a rating for and a Comment box they can type comments in. The issue I have right now is that while it all renders fine, I can't seem to make the Star Ratings clickable. i.e. If the user clicks on a star, it doesn't actually change the default value of 0 stars. If the user hovers over the stars, they animate fine. But it just doesn't seem to be selectable. Any thoughts? 

 

Star Rating Code in the Modal within render()

<StarRating
	rating={this.state.rating}
	starRatedColor="red"
	changeRating={() => this.changeRating.bind(this)}
	numberOfStars={5}
	name='rating'
/>

Constructor for the page

constructor() {
	super()
	this.state = {
		show: false,
		rating: 0
	}
}

 

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 17.2.1) | iPhone XR (iOS 17.2.1) | iPad Mini (iOS 9.3.5) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
Share on other sites

Link to post
Share on other sites

I think you need to change the prop to: 

changeRating={this.changeRating.bind(this, event)}

 

or bind the function in your constructor,

this.changeRating = this.changeRating.bind(this)

 

and then change the prop to

changeRating={() => this.changeRating(event)}

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×