Skip to content

Commit d379d9f

Browse files
committed
Refactor socket connection
1 parent 72044ce commit d379d9f

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
REACT_APP_API_URL=http://localhost:3000
1+
REACT_APP_API_URL=http://localhost:3001

src/App.jsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@ import io from 'socket.io-client';
33

44
const App = () => {
55
const [socket, setSocket] = useState(null);
6+
const [socketConnected, setSocketConnected] = useState(false);
67

78
useEffect(() => {
8-
setSocket(io(process.env.REACT_APP_API_URL));
9-
}, [setSocket]);
9+
const socket = io(process.env.REACT_APP_API_URL);
10+
socket.on('connect', () => {
11+
setSocketConnected(socket.connected);
12+
});
13+
// TODO: implement socket events handlers here
14+
setSocket(socket);
15+
return () => {
16+
socket.removeAllListeners();
17+
socket.close();
18+
};
19+
}, [setSocketConnected, setSocket]);
1020

11-
useEffect(
12-
() => () => {
13-
if (socket !== null) {
14-
socket.removeAllListeners();
15-
socket.close();
16-
}
17-
},
18-
[socket],
19-
);
20-
21-
if (!socket) {
21+
if (!socket || !socketConnected) {
2222
return <div>Loading...</div>;
2323
}
2424

25-
return <div>test</div>;
25+
return <div>Socket connected!</div>;
2626
};
2727

2828
export default App;

0 commit comments

Comments
 (0)