Hereโs a clear, beginner-friendly explanation of the 6 important React Hooks shown in the image. Each one is explained simply, with what it does, when to use it, and a short easy example.
1. useRef โ โA Drawer That Doesnโt Re-renderโ
What it does: Creates a box (reference) that can store a value and never causes your component to re-render when you change it.
Best for: Storing values that need to persist across renders (like timers, previous values, or direct access to DOM elements).
Simple Example:
const ref = useRef(null);
return <input ref={ref} />; // You can do ref.current.focus()
Key Point: Changing ref.current does not trigger a re-render.
2. useCallback โ โRemember Functionsโ
What it does: Remembers a function so it doesnโt get recreated on every render.
Best for: Passing functions to child components (prevents unnecessary re-renders of children).
Simple Example:
const memoizedFn = useCallback(() => {
console.log("Hello");
}, []); // [] = dependencies
Key Point: Without useCallback, every render creates a brand-new function โ children may re-render even when they donโt need to.
3. useState โ โStoring & Changing Valuesโ
What it does: Adds state (data that can change) to your component.
Best for: Anything that should make the UI update when it changes (count, form inputs, toggles, etc.).
Simple Example:
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count 1)}>{count}</button>;
Key Point: Updating state triggers a re-render of the component.
4. useContext โ โShare Data Without Prop Drillingโ
What it does: Lets you access data from a Context (global-like state) anywhere in your component tree.
Best for: Themes, user info, language settings โ data needed by many components.
Simple Example:
const theme = useContext(ThemeContext);
return <div style={{ color: theme }}>Hello</div>;
Key Point: No need to pass props through many levels of components.
5. useEffect โ โSyncing with Outside Worldโ
What it does: Runs side effects (code that interacts with the outside world) after render.
Best for: Fetching data, setting up subscriptions, manipulating DOM, timers.
Simple Example:
useEffect(() => {
fetchData().then(setData);
return () => cleanup(); // optional cleanup
}, [dependencies]);
Key Point: The dependency array [] controls when the effect runs.
6. useMemo โ โCache Expensive Calculationsโ
What it does: Remembers (caches) the result of an expensive calculation so it doesnโt run again unless dependencies change.
Best for: Heavy computations, filtering/sorting large lists, complex object creation.
Simple Example:
const expensiveValue = useMemo(() => {
return computeHeavyThing(data);
}, [data]);
Key Point: Improves performance by avoiding unnecessary recalculations.
Quick Summary Table (Easy to Remember)
Hook
Purpose
Causes Re-render?
Use When...
useRef
Store persistent value
No
Need value without re-render
useCallback
Remember functions
No (for the fn)
Passing functions to children
useState
Manage changing data
Yes
UI needs to update
useContext
Share global data
Yes (if value changes)
Avoid prop drilling
useEffect
Side effects (API, timers, etc.)
No (runs after)
Interact with outside world
useMemo
Cache expensive results
No
Heavy calculations
These 6 hooks cover most of what youโll need in modern React apps.
Would you like me to also create a simple code playground or mini project that combines all 6 hooks together? Just say the word!