Thanks in advance. I have a state array as below.
I need to add an item to state array, I came across that we need not do state mutation. How do i set state with prevSta
To insert new element at the end of the list
const addMessage = (newMessage) => setMessages(state => [...state, newMessage])
To insert new element at the begining of the list
const addMessage = (newMessage) => setMessages(state => [newMessage, ...state])