Home > Software design >  Why I cant push an object to my array existing array?
Why I cant push an object to my array existing array?

Time:02-04

Why I cant push this in my array ?

          setMessage(prevMsg => prevMsg.push({
            id: 9,
            user_id: 2,
            reciever: 1,
            text: 'Hallo, eine 5',
            images: [],
            video: null,
            sending: 0,
            pending: 0,
            read: 0,
            date: new Date()
          }))

......................................................................................................................................................................................................................................................................................................................

CodePudding user response:

This is not working because Array.push doesn't return what you want.

Instead, you can do this:

setMessage(prevMsg => [...prevMsg, {
  id: 9,
  user_id: 2,
  reciever: 1,
  text: 'Hallo, eine 5',
  images: [],
  video: null,
  sending: 0,
  pending: 0,
  read: 0,
  date: new Date()
}]))
  •  Tags:  
  • Related