InterviewHubPRO
Section Overview
Q269

Question 269 of 445

Part 9: React Hooks Deep Dive (262 - 316)

60%
React & Modern Frontend (445 Qs)•Part 9: React Hooks Deep Dive (262 - 316)

Can Hooks be used in class components?

No, Hooks cannot be used inside class components. They are specially designed for function components. This is because hooks depend on the sequence in which they are called during a component’s render, something that's only guaranteed in functional components. However, both class and function components can coexist in the same application.

Practical Code Example

jsx
// Hooks CANNOT be used in class components directly.
// Solution: Wrap class component in a functional component that passes hook values as props:
function withTheme(Component) {
  return function Wrapped(props) {
    const theme = useContext(ThemeContext);
    return <Component {...props} theme={theme} />;
  };
}