Power of React Hooks ?
" React Hooks are tools that allow you to use state and other React features without writing class components. "
They make your code simpler, cleaner, and easier to reuse. Hooks are a new addition in React 16.8. The current stable version of React is React 19.
Before Hooks, React developers used class components to handle state and lifecycle. With Hooks, we can now do all of that directly inside function components.
What is Hooks ?
React Hooks are like special tools that let you add features such as keeping track of data (state) and doing things when your component loads or updates, all from within function components. This means you can do a lot without needing to write class components.
Hooks are special functions in React that let you add extra features (like state or side effects) to functional components.
Think of them as tools that make functional components smarter.
👉 Each of these can be built easily with React Hooks (useState, useEffect, useRef).
Why Use Hooks?
Before learning Hooks, we should first understand why we need them
Types of React Hooks
React offers various hooks to handle state, side effects, and other functionalities in functional components. Below are some of the most commonly used types of React hooks:
1. State Hooks (useState & useReducer)
👉 useState & useReducer help functional components remember and update data (state).
useState (basic state) : Used when you just need to keep track of simple values (like a number, text, or boolean).
const [count, setCount] = useState(0);
useReducer (complex state) : Used when your state is more complicated (like objects, arrays, or many conditions).
const [state, dispatch] = useReducer(reducer, initialState);
React provides several built-in hooks, and you can also create your own.
2. useEffect – Handle side effects
👉 Runs code when something changes (like fetching data or updating the page title).
Example: Update document title when count changes.
useEffect(() => {
document.title = `Count: ${count}`;
}, [count]);
3. useContext – Share data without props
👉 Lets components use global values (like theme or user info).
Example: Dark mode theme toggle.
const theme = useContext(ThemeContext);
4. useReducer – Manage complex state
👉 Good for managing multiple values or state transitions.
Example: A todo list app.
const [state, dispatch] = useReducer(reducer, []);
5. useRef – Keep a reference
👉 Stores values that don’t re-render the UI (like DOM elements or timers).
Example: Focus on input automatically.
const inputRef = useRef(null);
useEffect(() => inputRef.current.focus(), []);
6. useMemo – Optimize performance
👉 Remembers calculated values so they don’t re-run unnecessarily.
Example: Expensive calculation.
const result = useMemo(() => slowFunction(num), [num]);
7. useCallback – Optimize functions
👉 Remembers a function so it doesn’t get recreated every render.
Example: Passing stable function to child.
const handleClick = useCallback(() => setCount(c => c + 1), []);
8. Custom Hooks
Imagine you want to track the window width in multiple components. Instead of writing the same logic everywhere, you create a custom hook:
useWindowWidth()
Basic Hooks - basic in short
Additional Hooks
Custom Hooks
📜 Rules of Hooks
Hooks are powerful, but they must be used correctly. Here are the rules:
React Hooks are one of the most powerful features in React. They:
Whether you’re a beginner or experienced developer, learning and applying Hooks will make your apps more modular, efficient, and easier to maintain.