Should You Write New Class Components?
What you'll learn: The definitive answer on whether you should choose class components when writing new React code today.
The Clear Answer: Almost Never
After learning about class components, their lifecycle methods, and how hooks replaced their patterns, you might wonder: "Should I write new components as classes?" The answer is straightforward: No, except for error boundaries.
The Only Exception
Error boundaries (componentDidCatch and getDerivedStateFromError) remain the sole feature that requires class components. React hooks currently have no equivalent. So if you need to catch errors in your component tree, you must write a class component—but typically just one or two per application.
Everything Else? Use Hooks
For all other scenarios:
- State management? Use
useState - Side effects? Use
useEffect - Performance optimization? Use
useMemoanduseCallback - Reusing logic? Write custom hooks
Hooks provide cleaner code, better logic organization, and easier testing. The React team has made it clear: hooks are the future, and classes are legacy.
The Practical Rule
When starting any new component, default to a function component with hooks. Only reach for a class when you're specifically implementing an error boundary. Even then, keep it minimal—just the error-catching logic—and use functional components everywhere else.
Key Takeaway: Write new components as functions with hooks by default. Only use classes when implementing error boundaries—the one feature hooks can't replace. Classes are for reading legacy code and maintaining existing systems, not for new development.