简体   繁体   中英

How to modify array within object in react component state

I have the following state in my app:

    const [activeChats, setActiveChats] = useState([
        {
          id: 1,
          name: 'Luciana Gutierrez',
          role: 'HR Manager',
          avatar: avatar2,
          messages: [
            {
              content: 'Hola Luciana! Tuviste alguna novedad?',
              received: false,
              date: '9:45 AM'
            },
            {
              content: 'Hola John! Todavía no...',
              received: true,
              date: '10:19 AM'
            },
            {
              content: 'Si tengo alguna otra novedad te comento. Gracias!',
              received: true,
              date: '10:20 AM'
            }
          ]
        },
        {
          id: 2,
          name: 'Micaela Alvarez',
          role: 'Marketing Manager',
          avatar: avatar1,
          messages: [
            {
              content: 'Hola John! Entre qué horarios podrías hoy tener la meeting?',
              received: true,
              date: '9:45 AM'
            },
            {
              content: 'Hola Micaela! Muy bien. Yo puedo de 10 a 17 hs.',
              received: false,
              date: '10:05 AM'
            },
            {
              content: 'Dale, agendé la meeting para hoy a las 14hs.',
              received: true,
              date: '10:15 AM'
            }
          ]
        },
        {
          id: 3,
          name: 'Manuel Hoffman',
          role: 'Business Manager',
          avatar: avatar3,
          messages: [
            {
              content: 'Gracias por la reunión de ayer, Manu, fue muy productiva!',
              received: false,
              date: '9:35 AM'
            },
            {
              content: 'Gracias a vos!',
              received: true,
              date: '9:37 AM'
            }
          ]
        }
      ])

And I want to modify it only by adding more items within the messages key of each object, leaving the rest of the state just as it was.

For example, lets say I add two new messages to the first object. The state should look like this:

const [activeChats, setActiveChats] = useState([
    {
      id: 1,
      name: 'Luciana Gutierrez',
      role: 'HR Manager',
      avatar: avatar2,
      messages: [
        {
          content: 'Hola Luciana! Tuviste alguna novedad?',
          received: false,
          date: '9:45 AM'
        },
        {
          content: 'Hola John! Todavía no...',
          received: true,
          date: '10:19 AM'
        },
        {
          content: 'Si tengo alguna otra novedad te comento. Gracias!',
          received: true,
          date: '10:20 AM'
        },
        {
          content: 'Example1',
          received: true,
          date: '10:21 AM'
        },
        {
          content: 'Example2',
          received: true,
          date: '10:22 AM'
        }
      ]
    },
    {
      id: 2,
      name: 'Micaela Alvarez',
      role: 'Marketing Manager',
      avatar: avatar1,
      messages: [
        {
          content: 'Hola John! Entre qué horarios podrías hoy tener la meeting?',
          received: true,
          date: '9:45 AM'
        },
        {
          content: 'Hola Micaela! Muy bien. Yo puedo de 10 a 17 hs.',
          received: false,
          date: '10:05 AM'
        },
        {
          content: 'Dale, agendé la meeting para hoy a las 14hs.',
          received: true,
          date: '10:15 AM'
        }
      ]
    },
    {
      id: 3,
      name: 'Manuel Hoffman',
      role: 'Business Manager',
      avatar: avatar3,
      messages: [
        {
          content: 'Gracias por la reunión de ayer, Manu, fue muy productiva!',
          received: false,
          date: '9:35 AM'
        },
        {
          content: 'Gracias a vos!',
          received: true,
          date: '9:37 AM'
        }
      ]
    }
  ])

How can I do this? Im thinking about the spread operator but I'm not sure how to use it with the nesting I have.

You need to return a new array for setActiveStates being particularly careful not to mutate the nested object that you are updating. A long hand approach is to findIndex() of the item you want to update, then use this index to spread slice() s of the array before and after the item in question, and also retrieve and clone the item itself and any nested object properties to return a copy.

const newMessages = [{ content: 'new1', received: true, date: '10:21 AM' }, { content: 'new2', received: true, date: '10:22 AM' }];
const itemId = 1;

setActiveChats(prevState => {
  const index = prevState.findIndex(({ id }) => id = itemId);
  const item = prevState[index];
  return [
    ...prevState.slice(0, index),
    { ...item, messages: [...item.messages, ...newMessages] },
    ...prevState.slice(index + 1)
  ];
});

It's a little bit tedious to do this with nested structures but something like this should work:

    setActiveChats(
      activeChats.map((activeChat, idx) => {
        // Add to the first entry
        if (idx === 0) {
          return {
            ...activeChat,
            // assumes `newMessages` is an array of new messages
            messages: [...activeChat.messages, ...newMessages]
          };
        }
        return activeChat;
      })
    );

See a full working example here .

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