Skip to main content

Command Palette

Search for a command to run...

React Hooks Explained with Real-Life Examples

Published
•4 min read•View as Markdown
React Hooks Explained with Real-Life Examples

React Hooks have revolutionized how we build components by allowing us to use state and lifecycle features in functional components without writing a class. But beyond the syntax, the true power of hooks lies in how naturally they map to real-life behaviour.

In this article, we'll explore the most commonly used React Hooks and explain them with relatable, real-world analogies.


1. useState — Storing a Value

React analogy: Remembering a value between renders.
Real-life analogy: Think of useState like a sticky note on your desk. It helps you remember something across the day.

Example:

const [count, setCount] = useState(0);

Imagine you're keeping score while playing table tennis. count is your current score, and setCount is your scoreboard marker.

Use cases:

  • Toggling a modal

  • Tracking form input

  • Managing a counter


2. useEffect — Side Effects

React analogy: Perform an action after a component renders.
Real-life analogy: Like setting an alarm after placing a coffee pot on the stove—you need to do something after something else has happened.

Example:

useEffect(() => {
  document.title = `You clicked ${count} times`;
}, [count]);

This effect runs after count changes. Like changing your mood after checking how many tasks you completed—it’s reactive.

Use cases:

  • API calls on mount

  • Updating DOM manually

  • Setting up event listeners


3. useContext — Sharing Global Data

React analogy: Access shared state across components.
Real-life analogy: Like a WiFi connection in a café. Everyone connects without plugging into each other directly.

Example:

const theme = useContext(ThemeContext);

No need to pass the theme down manually through props like a family heirloom. It’s just available to whoever needs it.

Use cases:

  • Theme toggling (dark/light)

  • Authentication status

  • Language or locale settings


4. useRef — Persisting a Mutable Value

React analogy: Store a value that doesn’t trigger re-renders.
Real-life analogy: Like a diary you write in but don’t show anyone—it’s just for your internal reference.

Example:

const inputRef = useRef(null);

You might use this to focus an input when a button is clicked. The component doesn’t care; it’s just something you want done directly.

Use cases:

  • Accessing DOM elements

  • Keeping timers

  • Storing previous values


5. useMemo — Caching Expensive Calculations

React analogy: Don’t redo the same heavy work unless inputs change.
Real-life analogy: Like a meal-prep box. You only cook if ingredients change.

Example:

const total = useMemo(() => calculateTotal(cart), [cart]);

If the cart hasn’t changed, we don't need to recalculate the total. Efficient, like reheating leftovers.

Use cases:

  • Avoid unnecessary calculations

  • Improve performance with large data

  • Optimizing render-heavy components


6. useCallback — Memoizing Functions

React analogy: Save a function reference unless its dependencies change.
Real-life analogy: Like reusing the same playlist unless your music taste changes.

Example:

const handleClick = useCallback(() => {
  console.log('Clicked!');
}, []);

useCallback keeps handleClick stable unless the things it depends on change.

Use cases:

  • Passing stable callbacks to child components

  • Preventing unnecessary renders

  • Keeping references consistent


7. useReducer — Managing Complex State

React analogy: An alternative to useState for complex logic.
Real-life analogy: Like a remote control—you send signals (actions) to change the TV's state.

Example:

const reducer = (state, action) => {
  switch (action.type) {
    case 'increment':
      return { count: state.count + 1 };
    default:
      return state;
  }
};

const [state, dispatch] = useReducer(reducer, { count: 0 });

You’re not changing the state directly. You dispatch actions, and the reducer interprets them—like flipping channels.

Use cases:

  • Form state with multiple fields

  • Game logic

  • Managing undo/redo functionality


Summary Table:

HookUse CaseReal-Life Analogy
useStateRemember a valueSticky note
useEffectPerform side effectsAlarm after cooking
useContextGlobal shared stateWiFi in a café
useRefPersistent referencePrivate diary
useMemoAvoid recalculating valuesMeal-prep leftovers
useCallbackAvoid recreating functionsReusing a playlist
useReducerHandle complex stateRemote control for TV

Final Thought

React Hooks make functional components as powerful as class components—if not more. Understanding them is like learning to use a toolbox: each tool has a specific use, and the more you use them, the more intuitive they become.

Next time you're coding and think, "I need to remember something," or "I want this to happen after that," reach into the hooks toolbox. There's probably a hook that fits just right.