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.

Uche Onuekwusi
Uche Onuekwusi
12,490 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)}