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 (2018) Managing State and Data Flow Adding Items to State

miguel soriano
miguel soriano
2,177 Points

my code is not working, can you let me know where I am wrong?

My code does not add the player that is typed on the form. this is my code in App. js: import React, {Component} from 'react'; import Header from './Header'; import Player from './Player'; import AddPlayerForm from './AddPlayerForm';

class App extends Component { state = { players: [ { name: "Guil", id: 1, score: 0,

  },
  {
    name: "Treasure",
    id: 2,
    score: 0,

  },
  {
    name: "Ashley",
    id: 3,
    score: 0,

  },
  {
    name: "James",
    id: 4,
    score: 0, 

  }
],

}; //player id counter prevPlayerID = 4 ;

handleRemovePlayer = (id) => { this.setState( prevState => { return { players: prevState.players.filter(p => p.id !== id) }; }); }

handleScoreChange = (index, change) => { this.setState( prevState => ({ score: prevState.players[index].score += change })); }

handleAddPlayer = (name) => {
  this.setState({
    players: [
      ...this.state.players,
      {
        name: name,
        score: 0,
        id: this.prevPlayerID += 1,
      }
    ]
  });
}

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

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

} }

export default App;

---------------------------------and this is my code in AddPlayerForm.js :

import React,{Component} from 'react';

class AddPlayerForm extends Component { state = { value: "", }

handleValueChange = (event) => {
    this.setState({
        value:event.target.value
    }
    )
}

handleSubmit = (e) => {
e.preventdefault();
this.props.addPlayer(this.state.value);
}

render(){
    return (
        <form onSubmit = {this.handleSubmit}>
            <input 
            type = "text"
            value = {this.state.value}
            onChange = {this.handleValueChange}
            placeholder = "Enter a player's name"
            />

            <input
            type = "submit"
            value = "Add Player"
            />
        </form>
    );

}

} export default AddPlayerForm;

Steph Lienti
Steph Lienti
8,046 Points

Thank you ...this helped me as I did the same thing! Reviewing line after line of code for four days....I had e.PreventDefault().

Thank you Miguel!

1 Answer

miguel soriano
miguel soriano
2,177 Points

Sorry, already saw the problem. My e.preventDefault() was e.preventdefault()