I tried to build this method addgamer() to check if the username exists in the gamers array onClick add button and if he is , i want it don't add his info as an object in the array, i want to know why it didn't work ? ....i created this enterdata() method to get the values of these three gamer info onChange the values of their inputs
class App extends Component {
state= {
gamer:{
FirstName:"",
LastName: "",
UserName: "",
},
gamers:[],
}
enterdata = (event)=> {
const {name, value} = event.target;
this.setState((prevstate)=>({
gamer: {...prevstate.gamer,[name] : value},
}))
}
addgamer = (event) =>{
event.preventDefault();
for(let i = 0;i<this.state.gamers.length; i++){
if(this.state.gamer.UserName === this.state.gamers[i].UserName){
return false;
}else{
this.setState(prevstate =>({
gamers: [...prevstate.gamers, this.state.gamer],
gamer: {FirstName:"", LastName: "", UserName: ""},
}))
}
}
}