3 ms·
State in React isn't exclusively reserved for UI interaction state like modals and dropdowns. It's also common to store server data in there if it should persis
by dceddia 10y ago
State in React isn't exclusively reserved for UI interaction state like modals and dropdowns. It's also common to store server data in there if it should persist between renders (which it usually should).
As another example, imagine that a chat app is receiving messages from a websocket. Each time it receives a message it should re-render the list of messages. The "current list of messages" is state. Each received message gets pushed onto an array inside state, which triggers a re-render.
That's not really "UI interaction state" in the sense that the user triggered it, but it's definitely state the UI needs to track unless it wants to fetch the entire list of messages every time a new one arrives.
- tmahle 10y agoI don't have experience using websockets with React, but right now I handle absolutely everything that comes from the server by putting it into app state. Is there a reason for changing that paradigm in the chat example?