>

CODEMASTERYLAB

IDE PLATFORM
CoursesCommunity BlogsKnowledge GraphQuizzesInterview Prep

Sign in to track progress & earn XP

Sign In Create Account
>
Code Mastery Lab

Code Mastery Lab

Sign In

State — Making Components Dynamic

Question 1 / 1

0:00
Core
Medium

A developer writes the following StarRating component:

class StarRating extends React.Component {
  constructor(props) {
    super(props)
    this.state = { starsSelected: 0 }
  }

  change(starsSelected) {
    this.state.starsSelected = starsSelected
  }

  render() {
    const { totalStars } = this.props
    const { starsSelected } = this.state
    return (
      <div>
        {[...Array(totalStars)].map((n, i) => (
          <Star key={i}
            selected={i < starsSelected}
            onClick={() => this.change(i + 1)}
          />
        ))}
        <p>{starsSelected} of {totalStars} stars</p>
      </div>
    )
  }
}

The user clicks stars but the display never updates. What is the root cause?