0
0
Reactframework~10 mins

Why custom hooks are used in React - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why custom hooks are used
Write logic in component
Notice repeated logic
Extract logic into custom hook
Use custom hook in multiple components
Share stateful logic cleanly
Simplify components and reuse code
This flow shows how repeated logic in components is extracted into a custom hook, which is then reused to share stateful logic cleanly.
Execution Sample
React
import { useState } from 'react';

function useCounter() {
  const [count, setCount] = useState(0);
  const increment = () => setCount(c => c + 1);
  return { count, increment };
}

function Counter() {
  const { count, increment } = useCounter();
  return <button onClick={increment}>{count}</button>;
}
This code defines a custom hook useCounter to manage count state and increment logic, then uses it inside a Counter component.
Execution Table
StepActionState BeforeState AfterComponent RenderedOutput
1Call useCounter inside CounterNo count statecount=0Counter<button>0</button>
2User clicks buttoncount=0count=1Counter<button>1</button>
3User clicks button againcount=1count=2Counter<button>2</button>
4Another component uses useCounterNo count statecount=0AnotherComponentInitial count 0
5User clicks button in AnotherComponentcount=0count=1AnotherComponentCount 1
6No more clickscount=1count=1No re-renderOutput stable
7ExitN/AN/AN/AUser stops interaction
💡 User stops interacting, no further state changes or renders
Variable Tracker
VariableStartAfter 1After 2After 3Final
count (Counter)undefined0122
count (AnotherComponent)undefinedundefinedundefinedundefined1
Key Moments - 2 Insights
Why do we extract logic into a custom hook instead of copying it into each component?
Extracting into a custom hook lets us reuse the same logic easily and keeps components simpler, as shown in execution_table rows 1 and 4 where different components use the same hook.
Does each component using the custom hook share the same state?
No, each call to the custom hook creates independent state, as seen in variable_tracker where count in Counter and AnotherComponent are separate.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the count value in the Counter component?
A0
B1
C2
DUndefined
💡 Hint
Check the 'State After' column for step 3 in execution_table
At which step does AnotherComponent first have count state initialized?
AStep 2
BStep 4
CStep 5
DStep 1
💡 Hint
Look for when AnotherComponent calls useCounter in execution_table
If we did not use a custom hook and copied logic into each component, what would change in variable_tracker?
Acount variables would still be independent but code would be duplicated
Bcount variables would be shared between components
Ccount variables would not exist
Dcount variables would be global
💡 Hint
Recall that custom hooks help reuse logic but each component keeps its own state as shown in variable_tracker
Concept Snapshot
Custom hooks let you extract and reuse stateful logic across components.
They keep components simple and avoid code duplication.
Each use of a custom hook has its own independent state.
Use them to share behavior, not state.
Syntax: function useMyHook() { const [state, setState] = useState(); ... return {...}; }
Full Transcript
Custom hooks in React are functions that let you reuse stateful logic across multiple components. Instead of copying the same code into each component, you extract it into a custom hook. Each component calling the hook gets its own independent state. This keeps components simpler and your code DRY (Don't Repeat Yourself). For example, a useCounter hook manages count state and increment logic. When used in different components, each has its own count. The execution table shows how state changes on button clicks and how components re-render. Key points are that custom hooks share logic, not state, and help organize code better.