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
Your state is an array so you will need to spread your previous state into a new array and add the new message using [...prevState, newMessage]
What you try to do is return an object, because {} is a code block so you need to wrap it inside () if you return an object which is what you are trying to do
setMessages(prevState => [...prevState, newMessage])