robtaussig / react-use-websocket

React Hook for WebSocket communication
MIT License
1.63k stars 135 forks source link

extra websocket is opened when react is run in development mode #119

Closed MaxBittker closed 2 years ago

MaxBittker commented 2 years ago

react renders components twice when it's run in strict mode, to help find accidental side effects, it looks like this hook does not close the old web-socket in that situation. (only effects share: false)

I think the solution would be returning a cleanup function at the end of this useEffect: https://github.com/robtaussig/react-use-websocket/blob/master/src/lib/use-websocket.ts#L139

See these issues:

https://github.com/facebook/create-react-app/issues/10387

https://github.com/facebook/react/issues/15074#issuecomment-471197572