DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

React Hooks Explained (Beginner-Friendly)

First: What is a Hook?

A Hook is just a special function React gives you to “plug into” React features like:

  • State
  • Lifecycle
  • Context
  • Performance optimization

Hooks always:

  • Start with use
  • Are called inside components only
  • Follow predictable rules

The Core Hooks (You should learn these)

These are the hooks you’ll use in almost every React app.


1️⃣ useState — Store changing data

You already know this one 👍

const [count, setCount] = useState(0);
Enter fullscreen mode Exit fullscreen mode

Use it for:

  • Form inputs
  • Toggles
  • Counters
  • Loading states

Mental model:

“Component-level variables that cause re-render when changed.”


2️⃣ useEffect — Run side effects

Also familiar now.

useEffect(() => {
  fetchData();
}, []);
Enter fullscreen mode Exit fullscreen mode

Use it for:

  • API calls
  • Subscriptions
  • Timers
  • Logging

Mental model:

“Run this after rendering or when something changes.”


3️⃣ useContext — Read global data

Used with React Context.

const user = useContext(UserContext);
Enter fullscreen mode Exit fullscreen mode

Use it for:

  • Logged-in user
  • Theme
  • Language

Mental model:

“Read shared data without passing props.”


Secondary Hooks (Learn after basics)

These make apps cleaner and faster.


4️⃣ useRef — Store values without re-rendering

const inputRef = useRef(null);
Enter fullscreen mode Exit fullscreen mode

Use it for:

  • Accessing DOM elements
  • Storing values that shouldn’t trigger UI updates
  • Timers, counters, previous values

Example:

inputRef.current.focus();
Enter fullscreen mode Exit fullscreen mode

Mental model:

“A box that holds data across renders without re-rendering.”


5️⃣ useReducer — Advanced state logic

Alternative to useState.

const [state, dispatch] = useReducer(reducer, initialState);
Enter fullscreen mode Exit fullscreen mode

Use it when:

  • State logic is complex
  • Many related state updates exist

Backend analogy:

Reducer = controller logic


6️⃣ useMemo — Cache expensive calculations

const result = useMemo(() => heavyCalculation(data), [data]);
Enter fullscreen mode Exit fullscreen mode

Use it to:

  • Prevent unnecessary recalculations
  • Improve performance

Mental model:

“Only recalculate if inputs change.”


7️⃣ useCallback — Cache functions

const handleClick = useCallback(() => {
  doSomething();
}, []);
Enter fullscreen mode Exit fullscreen mode

Use it when:

  • Passing functions to child components
  • Preventing unnecessary re-renders

Mental model:

“Same function instance unless dependencies change.”


Advanced / Rare Hooks (Know they exist)

You don’t need these early on, but it’s good to know them.


8️⃣ useLayoutEffect

Like useEffect, but runs before browser paint.

Use only when:

  • You need to measure layout sizes
  • You see flickering UI issues

⚠️ Rarely needed.


9️⃣ useImperativeHandle

Controls what a parent can access from a child component.

Used with forwardRef.

Advanced use cases only.


10️⃣ useId

Generates unique IDs.

const id = useId();
Enter fullscreen mode Exit fullscreen mode

Useful for:

  • Form inputs
  • Accessibility (labels)

Custom Hooks (VERY IMPORTANT)

You can create your own hooks.

function useGroups() {
  const [groups, setGroups] = useState([]);

  useEffect(() => {
    fetchGroups().then(setGroups);
  }, []);

  return groups;
}
Enter fullscreen mode Exit fullscreen mode

Use them to:

  • Reuse logic
  • Keep components clean

Mental model:

“Extract reusable logic into a function.”


Beginner Learning Order (Recommended)

Here’s the safe learning path:

  1. useState
  2. useEffect
  3. useContext
  4. useRef
  5. useReducer
  6. useMemo & useCallback
  7. Custom Hooks

What You Should NOT Do as a Beginner 🚫

  • Don’t memorize all hooks
  • Don’t use useMemo everywhere
  • Don’t optimize too early
  • Don’t jump to advanced hooks before basics

Quick Summary Table

Hook Purpose Learn When
useState Local state Day 1
useEffect Side effects Day 1
useContext Global state Early
useRef DOM & mutable values Early
useReducer Complex logic Intermediate
useMemo Performance Later
useCallback Stable functions Later

Final Takeaway

You don’t need all hooks to build real apps.
You need to understand a few deeply.

If you master:

  • useState
  • useEffect
  • useContext

You can build most production apps comfortably.

Top comments (0)