how to set state array using react hooks

前端 未结 5 849
情话喂你
情话喂你 2021-02-04 15:57

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

5条回答
  •  野性不改
    2021-02-04 16:39

    There is no real need to use the prevState, you could just do:

    setMessages([...messages, newMessage])
    

提交回复
热议问题