What are React Hooks ??
The Ultimate Guide for Beginners

React Hooks allow you to use state and lifecycle features in functional components without writing class components. Introduced in React 16.8, Hooks make React development more efficient and readable.
Letโs break down the most important Hooks and how to use them! ๐
๐น 1. useState โ Managing State
๐ useState lets you add state inside a functional component.
Example: Counter App
import { useState } from "react";
export default function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h1>Count: {count}</h1>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
๐น How it works?
countโ Holds the state valuesetCountโ Updates the stateuseState(0)โ Initializescountto 0
๐น 2. useEffect โ Side Effects in Components
๐ useEffect runs after the component renders. It's used for fetching data, updating the DOM, or setting up event listeners.
Example: Fetching Data
import { useState, useEffect } from "react";
export default function Users() {
const [users, setUsers] = useState([]);
useEffect(() => {
fetch("https://jsonplaceholder.typicode.com/users")
.then((res) => res.json())
.then((data) => setUsers(data));
}, []); // Empty array means it runs only on mount
return (
<ul>
{users.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}
๐น How it works?
Runs once when the component mounts (because of
[])Calls API and updates
usersstate
Variants of useEffect:
useEffect(() => { ... })โ Runs on every renderuseEffect(() => { ... }, [])โ Runs only once (on mount)useEffect(() => { ... }, [count])โ Runs when count changes
๐น 3. useRef โ Accessing DOM Elements & Preserving Values
๐ useRef is used for getting a reference to a DOM element or storing values without causing re-renders.
Example: Focusing an Input
import { useRef, useEffect } from "react";
export default function FocusInput() {
const inputRef = useRef(null);
useEffect(() => {
inputRef.current.focus(); // Auto-focus input field on mount
}, []);
return <input ref={inputRef} type="text" placeholder="Type here..." />;
}
๐น How it works?
useRef(null)โ Creates a mutable referenceinputRef.currentโ Directly accesses the input
๐น 4. useCallback โ Optimizing Functions
๐ useCallback memoizes functions so they donโt get re-created on every render, improving performance.
Example: Avoid Unnecessary Re-renders
import { useState, useCallback } from "react";
export default function Counter() {
const [count, setCount] = useState(0);
const increment = useCallback(() => {
setCount((prev) => prev + 1);
}, []);
return (
<div>
<h1>{count}</h1>
<button onClick={increment}>Increment</button>
</div>
);
}
๐น Why use useCallback?
Prevents
incrementfrom recreating on each renderImproves performance in child components
๐น 5. useMemo โ Optimizing Expensive Calculations
๐ useMemo caches a computed value so it doesnโt re-calculate unnecessarily.
Example: Expensive Calculation
import { useState, useMemo } from "react";
export default function ExpensiveCalculation() {
const [number, setNumber] = useState(0);
const squared = useMemo(() => {
console.log("Calculating square...");
return number * number;
}, [number]);
return (
<div>
<h1>Square: {squared}</h1>
<button onClick={() => setNumber((prev) => prev + 1)}>Increase</button>
</div>
);
}
๐น How it works?
Only re-computes when
numberchangesAvoids unnecessary calculations
๐น 6. useContext โ Global State Management
๐ useContext helps share global state without prop drilling.
Example: Theme Context
import { createContext, useContext } from "react";
const ThemeContext = createContext("light");
export function ThemeProvider({ children }) {
return <ThemeContext.Provider value="dark">{children}</ThemeContext.Provider>;
}
export function ThemedComponent() {
const theme = useContext(ThemeContext);
return <h1>Current Theme: {theme}</h1>;
}
๐น Why use useContext?
Avoids prop drilling
Shares state across multiple components
๐ Other Useful Hooks
useReducerโ LikeuseState, but for complex state logicuseImperativeHandleโ Exposes methods from a child componentuseLayoutEffectโ LikeuseEffect, but runs before the screen updates
๐ฏ Final Thoughts
Hooks simplify React development by making functional components more powerful. Mastering them will help you write cleaner, reusable, and optimized React apps!
๐ก Now, go build something amazing with React Hooks! ๐
Got questions? Drop them below! โฌ๏ธ Happy coding! ๐



