score:0

Accepted answer

following line will re-run every time your component renders, losing reference to the socket that was actually connected:

socket = io("http://localhost:3001", connectionoptions);

you can use a ref to persist it between renders:

const socketref = useref();
socketref.current = socket;
// use socketref.current everywhere else in your code

Related Query

More Query from same tag