You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//checks the state before it's updated so need to check the opposite condition
86
+
if(toggleSwitch){
87
+
//turn off
88
+
socket.off('remote cursor data from server');
89
+
//make remote cursor invisible
90
+
setRemoteCursors((prevState)=>{
91
+
constnewState=prevState.map((cursor)=>({
92
+
...cursor,
93
+
isVisible: false
94
+
}));
95
+
returnnewState;
96
+
});
97
+
}else{
98
+
//turn on
99
+
socket.on('remote cursor data from server',(remoteData)=>
100
+
handleCursorDataFromServer(remoteData)
101
+
);
102
+
//make remote cursor visible
103
+
setRemoteCursors((prevState)=>
104
+
prevState.map((cursor)=>({
105
+
...cursor,
106
+
isVisible: true
107
+
}))
108
+
);
109
+
}
110
+
};
86
111
87
-
//--------------------------------
112
+
console.log('Toggle Switch:',toggleSwitch);
113
+
114
+
constsocket=getSocket();
115
+
//wrap the socket event listener in useEffect with dependency array as [socket], so the the effect will run only when: 1. After the initial rendering of the component 2. Every time the socket instance changes(connect, disconnect)
116
+
useEffect(()=>{
117
+
console.log(
118
+
'socket inside useEffect:',
119
+
socket ? 'connected' : 'not connected'
120
+
);
121
+
122
+
if(socket){
123
+
console.log('------setting up socket.on event listener-------');
124
+
socket.on('remote cursor data from server',(remoteData)=>
125
+
handleCursorDataFromServer(remoteData)
126
+
);
127
+
}
128
+
129
+
return()=>{
130
+
console.log('clean up cursor event listener after canvas unmount');
131
+
if(socket)socket.off('remote cursor data from server');
132
+
};
133
+
},[socket]);
134
+
135
+
//-----------------
88
136
89
137
// find the current component based on the canvasFocus component ID in the state
0 commit comments