0
0
Reactframework~10 mins

Reusing logic with hooks in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reusing logic with hooks
Define custom hook
Use hook in component
Hook manages state & logic
Component renders with hook data
User interaction triggers hook update
Hook updates state
Component re-renders with new data
This flow shows how a custom hook encapsulates logic and state, which components can reuse to keep code clean and consistent.
Execution Sample
React
import { useState } from 'react';

function useCounter() {
  const [count, setCount] = useState(0);
  const increment = () => setCount(c => c + 1);
  return { count, increment };
}
This code defines a custom hook that manages a counter state and provides a function to increase it.
Execution Table
StepActionState BeforeState AfterComponent Render Output
1Call useCounter() in componentcount: undefinedcount: 0Render count: 0
2User clicks increment buttoncount: 0count: 1Render count: 1
3User clicks increment button againcount: 1count: 2Render count: 2
4No more clickscount: 2count: 2Render count: 2
💡 User stops clicking, state remains stable, no re-render triggered.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
countundefined0122
Key Moments - 3 Insights
Why does the component re-render when count changes?
Because the hook updates the state variable 'count', React detects this change and re-renders the component to show the new count value, as shown in steps 2 and 3 of the execution_table.
Can multiple components use the same custom hook independently?
Yes, each component calling the custom hook gets its own independent state. The hook logic is reused, but state is separate per component instance.
Why do we return an object with count and increment from the hook?
Returning an object bundles the state and the function to update it, so components can easily access and use both, keeping logic reusable and clear.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after step 2?
A0
B1
C2
Dundefined
💡 Hint
Check the 'State After' column in row for step 2.
At which step does the component first render with count equal to 0?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Component Render Output' column for each step.
If the increment function was not called, how would the execution_table change?
Acount would stay 0 and component renders once
Bcount would increase automatically
Ccomponent would render multiple times with same count
Dcount would become undefined
💡 Hint
Refer to the exit_note and steps where user clicks increment.
Concept Snapshot
Custom hooks let you reuse stateful logic across components.
Define a function starting with 'use' that uses React hooks inside.
Return state and functions to update it.
Use the hook in any component to share logic but keep state separate.
This keeps code clean and DRY (Don't Repeat Yourself).
Full Transcript
This lesson shows how to reuse logic in React components using custom hooks. We define a hook called useCounter that manages a count state and an increment function. When a component calls useCounter, it gets its own count state starting at zero. When the user clicks a button, the increment function updates the count, causing the component to re-render and show the new count. The execution table traces each step: initial render with count zero, then increments to 1 and 2 on clicks, with the component updating each time. Variables are tracked to show how count changes. Key moments clarify why components re-render on state change, how multiple components can use the same hook independently, and why the hook returns both state and updater. The quiz tests understanding of state values at steps and what happens if increment is not called. The snapshot summarizes how custom hooks help keep React code clean by reusing logic with separate state per component.