0

I need append a lot of object into my state var. Example:

[{
 name:'John',
 number: 9238
},
{
 name:'Fran',
 number: 5342
},
{
 name:'Peru',
 number: 6456
},
{
 name:'Marlene',
 number: 6456
}]

My code is this:

function PhoneBookForm() {
  const [users, setUser] = useState([{}]); 
  
  function SubmitHandler(e){
    e.preventDefault();
    
    setUser([
        {...users},
        { 
        name : e.target[0].value,
        number : e.target[1].value
        }
    ]);
  }
}

But if I console.log(users) in the end, only show me two arrays (one with the empty object, and the other with the last update. Doesn't append the inputs.

Marcos L
  • 3
  • 1
  • 1
    what If you remove the brackets from users? `setUser([ ...users, { name : e.target[0].value, number : e.target[1].value } ]);` – MWO Jan 12 '22 at 20:33

0 Answers0