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

cannot remove player

here is my code:

const Header = (props) => (
    <header>
        <h1>{props.title}</h1>
        <span className="stats">PLayers: {props.totalPlayers}</span>
    </header>
)

{/* instead of passing props.removePlayer() directly to the onClick event, you want to
pass in an anonymous function;
if you pass it directly, it will invoke once the page refreshes and all the players will
be deleted.
using anonymous function ensures that it invokes only when the event is triggered*/}
const Player = (props) => {
  return (
    <div className="player">
      <span className="player-name">
        <button className="remove-player" onClick={() => props.removePlayer(props.id)}></button>
        {props.name}
      </span>
      <Counter/>
    </div>
  )
}

class Counter extends React.Component {
   state = {
      score : 0
   }
   incrementScore = () => {
      this.setState( prevState => ({
         score: prevState.score + 1
      }));
   }

   decrementScore = () => {
      this.setState( prevState => ({
         score: prevState.score - 1
      }));
   }
   render() {
     return (
      <div className="counter">
          <button className="counter-action decrement" onClick={this.decrementScore}> - </button>
          <span className="counter-score">{this.state.score}</span>
          <button className="counter-action increment" onClick={this.incrementScore}> + </button>
      </div>
     )
   }
}



class App extends React.Component {
  state = {
    players: [
      {
        name: "Guil",
        id: 1
      },
      {
        name: "Treasure",
        id: 2
      },
      {
        name: "Ashley",
        id: 3
      },
      {
        name: "James",
        id: 4
      }
    ]
  }

  handleRemovePlayer = id => {
    this.setState(prevState => {
      return {
        player: prevState.players.filter(p => p.id !== id)
      }
    })
    console.log('player successfully removed')
  }
   render() {
      return (
         <div className="scoreboard">
            <Header
               title="Scoreboard"
               totalPlayers = {this.state.players.length}
            />
         {/* Players list */}
         {this.state.players.map( player =>
            <Player
                name={player.name}
                id = {player.id}
                key={player.id.toString()}
                removePlayer = {this.handleRemovePlayer}
                /* giving the player a method from the parent function means that
                * player is now able to send data back to its parent App component
                * through the callback function that's being passed to it through props*/
            />
          )}
         </div>
      )
  }
}



ReactDOM.render(
    <App />,
    document.getElementById('root')
)

It does not throw any error, and the console logs 'player successfully removed' (i just put the console.log there for debugging), but the players won't get removed. I do not understand what is wrong

1 Answer

Heidi Fryzell
seal-mask
MOD
.a{fill-rule:evenodd;}techdegree seal-36
Heidi Fryzell
Front End Web Development Treehouse Moderator 25,178 Points

Hi Bryan,

Is this from a particular challenge within one of the courses? If it is you will get more answers if you link to the challenge.

I think the easiest way to do this is from within the challenge, there is a "get hints" button at the top right and if the hints don't help then there is a button to "post in discussion" and if you create your question from that location the people reading your question will see a link to the challenge and will know which challenge you are referring too.

I hope this is helpful and happy coding!

Heidi