I faced an unordinary javascript functionality. Below, you will see React code (more precisely: useEffect hook) compiled to javascript.
(0, _react.useEffect)(() => {
console.log("Outside", socket);
if (socket !== null && socket !== void 0 && socket.connected) {
console.log("Inside", socket);
setNewSocket(socket);
}
}, [props]);
you can see two console.log's, one outside 'if' statement and one inside.
However, in browser I see only 'Outside' console.log regardless if socket appear or not:
When I removing last 'if' check (&& socket.connected), the statement passes, but socket object and its keys values does not match.
code:
(0, _react.useEffect)(() => {
console.log("Outside", socket);
if (socket !== null && socket !== void 0 ) {
console.log("Inside", socket);
console.log("connected", socket.connected);
setNewSocket(socket);
}
}, [props]);
Problem is that as a result setNewSocket setting socket with bad values (with socket.connected: false) causes the socket not to work.
How can it be possible? Any ideas?
one more interesting functionality is this behaviour happens only when I going directly to the page where socket is used, exp.: https://myweb.com/tableWithSocket. However, if I go to the /home page and then to the /tableWithSoket via navigation menu, it seems to work ok. Also, when I resize my page via toggle device toolbar or when I close my navbar, it starts to work as well. In conclusion, it guess it starts to work when rerender happens