0
0
Reactframework~10 mins

Creating custom hooks in React - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating custom hooks
Start Component
Call Custom Hook
Inside Hook: useState/useEffect
Return State/Functions
Component Uses Hook Data
Render UI with Hook Data
User Interaction -> State Update
Re-render Component with New State
The component calls the custom hook, which manages state and effects, returns data and functions, and the component uses these to render and update UI.
Execution Sample
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>;
}
A custom hook useCounter manages a count state and increment function, used by Counter component to display and update count on button click.
Execution Table
StepActionState BeforeState AfterComponent Render Output
1Component Counter mounts, calls useCountercount: undefinedcount: 0<button>0</button>
2User clicks button, increment calledcount: 0count: 1<button>1</button>
3Component re-renders with updated countcount: 1count: 1<button>1</button>
4User clicks button again, increment calledcount: 1count: 2<button>2</button>
5Component re-renders with updated countcount: 2count: 2<button>2</button>
6No more clicks, component stablecount: 2count: 2<button>2</button>
💡 Execution stops when user stops interacting; state remains stable.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
countundefined0122
Key Moments - 3 Insights
Why does the component re-render after calling increment?
Because increment updates the state 'count' inside the custom hook, React triggers a re-render to update the UI, as shown in steps 2 and 3 of the execution_table.
Is the custom hook called multiple times or just once?
The custom hook is called on every render of the component, but React preserves the state between renders. This is why 'count' updates correctly as seen in the variable_tracker.
Can the custom hook return anything other than state and functions?
Yes, it can return any value needed by the component, but typically it returns state and functions to manage that state, as shown in the example where it returns { count, increment }.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after Step 2?
A1
B0
C2
Dundefined
💡 Hint
Check the 'State After' column for Step 2 in the execution_table.
At which step does the component first render with count equal to 0?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Component Render Output' column in the execution_table.
If the increment function did not update state, what would happen to the component render output?
AIt would update normally
BIt would never re-render with new count
CIt would crash
DIt would show an error message
💡 Hint
Refer to how state changes trigger re-renders in the execution_table and key_moments.
Concept Snapshot
Creating custom hooks:
- Define a function starting with 'use'
- Use React hooks inside (useState, useEffect)
- Return state and functions
- Call hook inside functional components
- State updates trigger re-renders
- Reuse logic cleanly across components
Full Transcript
This visual execution shows how a React component uses a custom hook to manage state. The custom hook useCounter initializes a count state to zero and provides an increment function. When the component mounts, it calls useCounter, which returns the current count and increment. The component renders a button showing the count. When the user clicks the button, increment updates the count state inside the hook. React detects this state change and re-renders the component with the new count value. The execution table traces each step: mounting, clicking, state updates, and re-renders. The variable tracker shows how the count changes from undefined to 0, then increments with each click. Key moments clarify why re-renders happen and how hooks preserve state across renders. The quiz tests understanding of state values at steps and the effect of state updates on rendering. The snapshot summarizes how to create and use custom hooks to share logic and state in React components.