“The best way to understand something is to break it down to its simplest form.”
React Hooks were introduced in React 16.8, changing how we write components forever. They allow you to use state and lifecycle features without writing a class. Let’s explore them in a beginner-friendly, practical way.
🧠 Why Hooks?
Before Hooks, you had to use class components to manage state and side effects. With Hooks, functional components became just as powerful — and a lot cleaner.
“Simplicity does not precede complexity, but follows it.” — Alan Perlis
🔧 Commonly Used Hooks
1.
useState()
— State in Functional Componentsconst [count, setCount] = useState(0);
It returns a stateful value and a function to update it.
2.
useEffect()
— Side Effects (e.g., API calls, DOM updates)useEffect(() => { console.log("Component mounted or updated"); }, [dependency]);
Think of it as
componentDidMount
,componentDidUpdate
, andcomponentWillUnmount
in one.3.
useContext()
— Access Global Data (without prop drilling)const theme = useContext(ThemeContext);
It helps you consume values from React Context easily.
4.
useRef()
— Mutable Values That Persistconst inputRef = useRef(null);
It’s useful for accessing DOM nodes or keeping mutable variables that don’t trigger re-renders.
5.
useReducer()
— Advanced State Managementconst [state, dispatch] = useReducer(reducer, initialState);
Great for complex state logic, often used with global state tools.
🎯 Custom Hooks — Reusable Logic
You can build your own hooks to reuse logic between components.
function useWindowWidth() { const [width, setWidth] = useState(window.innerWidth); useEffect(() => { const handleResize = () => setWidth(window.innerWidth); window.addEventListener("resize", handleResize); return () => window.removeEventListener("resize", handleResize); }, []); return width; }
“Don’t Repeat Yourself — make a custom hook instead.” — Every React Dev
✅ Final Thoughts
React Hooks make code cleaner, reusable, and easier to reason about.
Once you master them, your productivity as a React developer skyrockets 🚀“Hooks are not magic. They’re just functions — but really powerful ones.”