Wierd react useEffect and socketio behaviour

Solution 1:

io has side effects. You create a connection everytime you call it. Because you call it in the render phase, everytime the state of the component is updated, a new connection will be established.

The easiest way to prevent that would be to memoize it.

const socketio = React.useMemo(() => io('http://localhost:5000'), [])

or move it to be part of the state of the component, and connect in a useEffect.