Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript React Components 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
16,759 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)}