Hooks are special functions introduced in React 16.8 that let you use state and other React features without writing a class. They make functional components powerful and easier to maintain by providing lifecycle and state management features directly inside them.
React provides several built-in hooks — the most common are useState, useEffect, and useRef. Hooks also enable developers to build custom reusable logic for cleaner, modular code that can be shared across components.
useState).useEffect).useRef).useMemo, useCallback).useContext).useReducer).
// ✅ Correct: hook used at the top level of the component
function Example() {
const [count, setCount] = React.useState(0);
}
// ❌ Incorrect: do NOT call hooks inside conditionals
if (true) {
const [count, setCount] = React.useState(0);
}
Use useState to store and update local state (like a counter value).
import React, { useState } from "react";
// Simple counter component using useState
function Counter() {
const [count, setCount] = useState(0);
return (
<div className="text-center">
<h4>Count: {count}</h4>
<button
className="btn btn-primary me-2"
onClick={() => setCount(count + 1)}
>
+
</button>
<button
className="btn btn-secondary"
onClick={() => setCount(0)}
>
Reset
</button>
</div>
);
}
export default Counter;
Use useEffect to run side effects like timers, API calls, or subscriptions.
import React, { useState, useEffect } from "react";
// Timer that increases every second using useEffect
function Timer() {
const [time, setTime] = useState(0);
useEffect(() => {
const interval = setInterval(() => {
setTime((t) => t + 1);
}, 1000);
return () => {
clearInterval(interval);
};
}, []);
return <p>Time Elapsed: {time}s</p>;
}
export default Timer;
Use useRef when you want to directly access DOM elements or store mutable values.
import React, { useRef } from "react";
// Focus an input element using useRef
function FocusInput() {
const inputRef = useRef(null);
const handleFocus = () => {
if (inputRef.current) {
inputRef.current.focus();
}
};
return (
<div>
<input
ref={inputRef}
placeholder="Type something..."
className="form-control mb-2"
/>
<button
className="btn btn-success"
onClick={handleFocus}
>
Focus Input
</button>
</div>
);
}
export default FocusInput;
Together, these examples show how useState, useEffect, and useRef work to manage state, side effects, and DOM access in React functional components.
| Hook | Purpose | Runs On |
|---|---|---|
useState |
Manage local component state | Every re-render when state changes |
useEffect |
Run side effects (fetch, timer, etc.) | After render |
useRef |
Access DOM nodes or persist values | Does not trigger re-render |
useContext |
Access shared data from Context | Whenever context value changes |
useReducer |
Manage complex state transitions | When an action is dispatched |
useEffect but runs synchronously after all DOM mutations.useEffect with useState for dynamic updates.useRef to store values that persist across renders without triggering re-renders.useEffect return functions.use so they follow React’s conventions.useState (with increment, decrement, and reset).useEffect that updates every second and stops when a button is clicked.useRef to focus an input field automatically on load or on a button click.useState and useEffect to track online/offline status using the window online/offline events.useContext for theme switching and useReducer for managing a to-do list.Goal: Understand the purpose and use of React’s core hooks to manage state, handle side effects, and interact with the DOM effectively within functional components.