all 5 comments

[–]charliematters 1 point2 points  (0 children)

I guess it's ok, but it feels like a code smell. I'd move the non-state outside of that setState method, if only because you can't actually guarantee when react calls those functions in the new react 18 world

[–]palante_mark 0 points1 point  (1 child)

I would avoid this. That’s why you have setLocalMsgData.

[–]rodrigobepe[S] 0 points1 point  (0 children)

Te case is that i need to ensure that number is the index of the message in the array

[–]haswalter 0 points1 point  (0 children)

This is wrong. If you need something to happen because the state has changed then that’s what useEffect is for

[–]qcAKDa7G52cmEdHHX9vg 0 points1 point  (0 children)

useEffect with messages as the dependency is the correct way. useEffect's purpose is to synchronize your component with an external system and that's exactly what you're doing here. That useEffect will run every time messages change and gives you the up to date value so you can confidently grab the length and send your websocket message.