What are React Hooks and why were they introduced?
Full Answer
React Hooks are functions that let you use React state and lifecycle features inside functional components, without writing a class component.
Introduced in React 16.8, they solve three main problems with class components:
-
Reusing stateful logic — before Hooks, sharing stateful logic required patterns like render props or HOCs. Hooks let you extract stateful logic into a custom Hook and reuse it directly.
-
Complex components — lifecycle methods like
componentDidMountforced unrelated logic to live together.useEffectlets you co-locate related side effects. -
Classes are confusing —
thisbinding and event handler binding created a learning curve. Hooks eliminate all of that.
Core Hooks: useState, useEffect, useContext, useRef, useMemo, useCallback
Rules of Hooks:
- Only call Hooks at the top level
- Only call Hooks from React functions
Quick Answer for Interviewer
Hooks let functional components use state and lifecycle features previously only available in class components. Introduced in React 16.8 to simplify code reuse, co-locate related logic, and eliminate class complexity.
Flashcard