Common Class Component Mistakes Developers Still Make
Class components have a predictable set of mistakes. Here are the most common ones, useful for reading legacy code and interviews.
Common Class Component Mistakes Developers Still Make
Even though new code uses hooks, class component mistakes still appear in legacy code and interviews. Here are the most common ones.
Forgetting to Bind Methods
Class methods that use this lose their binding when passed as callbacks, so this is undefined. You must bind in the constructor or use arrow function class fields.
Mutating State Directly
Writing this.state.value = 5 instead of this.setState does not trigger a re-render. Always use this.setState to update state.
Using this.setState Synchronously
this.setState is asynchronous. Reading this.state right after calling it shows the old value. Pass a function if the new state depends on the previous.
Forgetting Cleanup in componentWillUnmount
Setting up a subscription or timer in componentDidMount but forgetting to clean it up in componentWillUnmount causes leaks.
Fetching in render
Fetching or mutating state directly in render causes infinite loops and inconsistent state. Side effects belong in lifecycle methods, never in render.
Not Handling Prop Changes in componentDidUpdate
When a prop changes and you need to refetch, you must do it in componentDidUpdate by comparing prevProps to current props. Forgetting this leaves stale data.
Calling setState on an Unmounted Component
If an async callback calls setState after unmount, React warns. Track mounted state or cancel async work in componentWillUnmount.
The Takeaway
Class component mistakes include unbound methods, direct state mutation, treating setState as synchronous, missing cleanup, side effects in render, and stale data from ignored prop changes. Knowing these helps you read and fix legacy code.
Because in JavaScript, this is determined by how a function is called, not where it is defined. When you pass a class method as a callback, it loses its binding and this becomes undefined. You must bind in the constructor or use arrow function class fields.
Because React tracks state changes through this.setState. Mutating this.state directly does not trigger a re-render, so the UI does not update. Always use this.setState to update state in class components.
No. this.setState is asynchronous and batched. Reading this.state right after calling it shows the old value. If the new state depends on the previous, pass a function to this.setState to get the correct previous value.
To clean up anything you set up in componentDidMount, like subscriptions, timers, and listeners. Forgetting cleanup causes memory leaks and warnings about updating state on an unmounted component.
In componentDidUpdate, compare prevProps to current props. If the relevant prop changed, refetch. Forgetting this leaves stale data when the prop changes, similar to forgetting a dependency in useEffect.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

