简体   繁体   中英

Why I cant push an object to my array existing array?

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()
          }))

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

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()
}]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM