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 React Components (2018) Managing State and Data Flow Update State Based on a Player's Index

My Delta Is undefined

handleScoreChange = (index, delta) => { this.setState( prevState => ({ score: prevState.players[index].score += delta })); console.log('index:'+ index, 'delta:' + delta); } handleRemovePlayer = (id) => { this.setState( prevState => { return { players: prevState.players.filter(p => p.id !== id) }; }); }

render() { return ( <div className="scoreboard"> <Header title="Scoreboard" totalPlayers={this.state.players.length} />

    {/* Players list */}
    {this.state.players.map( (player, index)=>
      <Player
        name={player.name}
        score={player.score}
        id={player.id}
        key={player.id.toString()}
        index={index}
        changeScore={this.handleScoreChange}
        removePlayer={this.handleRemovePlayer}
      />
    )}
  </div>
);

} }

This is just some of my code I keep getting the error that Delta is undefined when I add a the index to the function parameters.

1 Answer

Uche Onuekwusi
Uche Onuekwusi
17,817 Points

check both your player and counter components. The index props must passed in at the counter component mentioned in the player component module. Then you store let index = props.index in the counter component and include it in in your onclick function call in the counter module. like this : onClick = {()=>props.changeScore(index,-1)}