Hooks (React 16.8+) let function components hold state, run effects and access React features without classes. Their big win is reusing stateful logic through custom hooks (no more HOC and render-prop "wrapper hell"). They also co-locate related logic that classes spread across lifecycle methods, avoid this binding confusion, and produce code that is easier to compose, minify and optimize. That includes the React Compiler, which only works on function components and hooks.
Before hooks, sharing behaviour such as "subscribe to window size" required:
withWindowSize(withRouter(withTheme(Component))). Wrapper nesting, prop-name collisions, and unclear origin of props.<WindowSize>{size => …}</WindowSize>. Nested callback pyramids.componentDidMount, componentDidUpdate and componentWillUnmount, with unrelated concerns mixed in the same method.function useWindowWidth() {
const [width, setWidth] = useState(() => window.innerWidth);
useEffect(() => {
const onResize = () => setWidth(window.innerWidth);
window.addEventListener('resize', onResize);
return () => window.removeEventListener('resize', onResize);
}, []);
return width;
}
function Sidebar() {
const width = useWindowWidth();
return width > 900 ? <FullNav /> : <CompactNav />;
}
Setup and cleanup sit together. The hook is a plain function you can test, compose and share, and it adds no wrapper components to the tree.
useEffect per side effect, rather than one lifecycle method holding many concerns.this. No binding handlers or this.state vs this.props mix-ups.useAuth from useContext + useSyncExternalStore + useEffect.eslint-plugin-react-hooks) catch missing dependencies and rule violations, functions minify better than classes, and types are simpler.useTransition, useDeferredValue), useId, useSyncExternalStore, React 19's use, useActionState, useOptimistic, and the React Compiler are all hooks-first.componentDidCatch/getDerivedStateFromError) don't have a hook equivalent, even in React 19.Spotted a mistake or a rights concern? Report an issue