No Way to Subscribe to Multiple Sources Cleanly
What you'll learn: How class lifecycle methods forced you to jam unrelated subscriptions into one messy method.
The Problem
In class components, componentDidMount was your only reliable place to set up side effects after the first render. This sounds fine until your component needs to:
- Subscribe to a WebSocket
- Set up an interval timer
- Fetch data from an API
- Listen to browser events
- Subscribe to a Redux store
All of these unrelated concerns had to live in the same componentDidMount method. Worse, their cleanup logic had to be separated into componentWillUnmount, creating a "split-brain" problem where related code lived in different methods.
The Sprawl
Imagine a chat component:
componentDidMount() {
// WebSocket subscription
this.socket = connectWebSocket();
this.socket.on('message', this.handleMessage);
// Typing indicator timeout
this.typingTimer = setInterval(this.checkTyping, 1000);
// Online status
ChatAPI.subscribeToUserStatus(this.props.userId, this.handleStatus);
// Window focus detection
window.addEventListener('focus', this.handleFocus);
}
componentWillUnmount() {
this.socket.disconnect();
clearInterval(this.typingTimer);
ChatAPI.unsubscribeFromUserStatus(this.props.userId, this.handleStatus);
window.removeEventListener('focus', this.handleFocus);
}
Every new subscription adds two lines in two different places. Related logic is physically separated, making it easy to forget cleanup or mismatch setup/teardown pairs.
Key Takeaway: Class lifecycles had no mechanism to group related subscription logic together, forcing developers to scatter setup and cleanup across different methods, which created fragile, hard-to-maintain code—especially as components grew more complex.