samedi 6 mars 2021

ReactJs program to add in array of objects if name already doesnt exist and if it exist alert the name already exist?

The alert doesn't come up the first time i submit a repeated value and the alert doesn't show the name

const addName = (event) => {
  event.preventDefault()
  const nameObject = {
    name: newName
  }
  const temp = persons.filter(
    person => person.name === nameObject.name
  )

  if (temp === undefined || temp.length === 0) {
    setPersons(persons.concat(nameObject))
    setNewName('')  
  } else {
    console.log(nameObject.name)
    alert(`${nameObject.name} already exists in phonebook`)
  }  
}



Aucun commentaire:

Enregistrer un commentaire