When using socket.io-client in react, the this direction inside the switching compenent, will be changed. What is the cause?

when using socket.io-client in react, the this direction inside the switching compenent, will be changed. What is the cause?

my socket.on is registered in compnentDidMount, but it is not destroyed when the component is removed, and the this point inside the callback time is retained. I tried to assign this to that, at the top of the componentDidMount, but the that.setState still does not take effect

Mar.31,2021

can match socket to window , so it becomes global, and you can modify it at any time.
it is recommended that the value of onmessage of socket be placed in state of redux , not in the state of the component.
if you just use socket in the component, drop websocket close when componentWillUnmount .

Menu