Effect cleanup

Effect cleanup is the function returned by a React useEffect that releases resources or reverses work performed by the effect. In this course, cleanup marks the socket lifecycle as stopped, cancels pending reconnection timers, and closes the active WebSocket when the component unmounts or its dependencies change.

Minimal example

tsx
useEffect(() => {
  const socket = new WebSocket(url);
  return () => {
    socket.close();
  };
}, [url]);

Learn this concept

Lesson 5.10 · Reconnecting WebSocket hooks

Used in exercises

Exercise 5.12 · Rider mode and customer tracking screens