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

Jannick Moth
Jannick Moth
8,714 Points

i is only returning 6. What's going on?

Hi, I'm working through the practice state in react challenge. Video 3, I can't get the solution to work. The state only updates to 5, it doesn't update to any other value. I've been looking around and I can't figure out what's going on. Below is the function I've created and then under that is the component it's passed to in props.

createStarComponents = () => {
    var starArray = [];
    var maxRating = 5;

    for ( var i = 0; i < maxRating; i ++ ) {
      starArray.push( 
      <Star 
        starHandler={ () => this.starHandler( i ) }
        key={i} 
      /> );
    }
    return starArray;
  }
const Star = (props) =>
  // 1. Call the function that updates the rating state each time a list item is clicked
  // 2. Give the <li> the class 'selected' if it's one of the selected stars
  <li onClick={props.starHandler}>
    <svg x="0px" y="0px"
    viewBox="0 0 16 15" className="star" >
      <path d="M8.5,0.3l2,4.1c0.1,0.2,0.2,0.3,0.4,0.3l4.6,0.7c0.4,0.1,0.6,0.6,0.3,0.9l-3.3,3.2c-0.1,0.1-0.2,0.3-0.2,0.5l0.8,4.5
      c0.1,0.4-0.4,0.8-0.8,0.6l-4.1-2.1c-0.2-0.1-0.3-0.1-0.5,0l-4.1,2.1c-0.4,0.2-0.9-0.1-0.8-0.6l0.8-4.5c0-0.2,0-0.4-0.2-0.5L0.2,6.2
      C-0.2,5.9,0,5.4,0.5,5.3L5,4.7c0.2,0,0.3-0.1,0.4-0.3l2-4.1C7.7-0.1,8.3-0.1,8.5,0.3z"/>
    </svg>
  </li>;

1 Answer

Jannick Moth
Jannick Moth
8,714 Points

Hi Clayton, thanks for your comment. I also tried it without maxRating and just declaring i n the for loop and still have the same problem