Bird
Raised Fist0
Reactframework~8 mins

Creating custom hooks in React - Performance Optimization Steps

Choose your learning style10 modes available
Performance: Creating custom hooks
MEDIUM IMPACT
Custom hooks affect component rendering speed and bundle size by reusing logic efficiently and avoiding unnecessary re-renders.
Reusing stateful logic across multiple components
React
function useCounter() {
  const [count, setCount] = useState(0);
  useEffect(() => {
    const id = setInterval(() => setCount(c => c + 1), 1000);
    return () => clearInterval(id);
  }, []);
  return count;
}

function Component() {
  const count = useCounter();
  return <div>{count}</div>;
}
Extracting logic into a custom hook reduces duplication and centralizes updates, improving bundle size and render consistency.
📈 Performance GainSaves bundle size and avoids redundant effect setups, reducing render overhead.
Reusing stateful logic across multiple components
React
function Component() {
  const [count, setCount] = useState(0);
  useEffect(() => {
    const id = setInterval(() => setCount(c => c + 1), 1000);
    return () => clearInterval(id);
  }, []);
  return <div>{count}</div>;
}

// Repeated in multiple components without abstraction
Duplicated logic in each component causes larger bundle size and harder maintenance.
📉 Performance CostAdds extra bytes to bundle and risks inconsistent updates causing unnecessary re-renders.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Duplicated logic in componentsMultiple effect setupsMultiple reflows per componentHigher paint cost due to repeated updates[X] Bad
Custom hook with stable dependenciesSingle effect setup reusedSingle reflow per updateLower paint cost with fewer updates[OK] Good
Rendering Pipeline
Custom hooks run during React's render phase to prepare state and effects. Proper design minimizes extra state updates and effect executions, reducing layout recalculations and paints.
→Render
→Commit
→Layout
→Paint
⚠️ BottleneckExcessive state updates inside hooks causing repeated Layout and Paint stages.
Core Web Vital Affected
INP
Custom hooks affect component rendering speed and bundle size by reusing logic efficiently and avoiding unnecessary re-renders.
Optimization Tips
1Extract reusable logic into custom hooks to reduce code duplication and bundle size.
2Use stable primitive values as dependencies to avoid unnecessary effect executions.
3Avoid side effects inside hooks that cause frequent state updates triggering re-renders.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a key performance benefit of using custom hooks in React?
AThey automatically cache all API responses.
BThey reduce code duplication and prevent unnecessary re-renders.
CThey increase bundle size by adding more code.
DThey force components to render synchronously.
DevTools: React DevTools Profiler
How to check: Record a profiling session while interacting with components using custom hooks. Look for repeated renders and effect executions.
What to look for: High render counts or frequent effect calls indicate inefficient hooks causing performance issues.

Practice

(1/5)
1. What is the main reason to create a custom hook in React?
easy
A. To create new HTML elements
B. To reuse stateful logic across multiple components
C. To style components with CSS
D. To replace React components entirely

Solution

  1. Step 1: Understand the purpose of hooks

    Hooks let you use React features like state and effects inside functions.
  2. Step 2: Identify why custom hooks are used

    Custom hooks let you reuse stateful logic easily across components without repeating code.
  3. Final Answer:

    To reuse stateful logic across multiple components -> Option B
  4. Quick Check:

    Custom hooks = reuse logic [OK]
Hint: Custom hooks always reuse logic, not UI or styles [OK]
Common Mistakes:
  • Thinking custom hooks create UI elements
  • Confusing hooks with styling tools
  • Believing hooks replace components
2. Which of the following is the correct way to name a custom hook in React?
easy
A. hookUseFetch
B. fetchDataHook
C. FetchData
D. useFetchData

Solution

  1. Step 1: Recall naming rules for custom hooks

    Custom hooks must start with the word "use" to follow React conventions.
  2. Step 2: Check each option

    Only "useFetchData" starts with "use" correctly.
  3. Final Answer:

    useFetchData -> Option D
  4. Quick Check:

    Custom hook names start with "use" [OK]
Hint: Custom hooks always start with 'use' prefix [OK]
Common Mistakes:
  • Naming hooks without 'use' prefix
  • Using PascalCase without 'use'
  • Placing 'use' in the middle or end
3. Consider this custom hook code:
import { useState, useEffect } from 'react';

function useWindowWidth() {
  const [width, setWidth] = useState(window.innerWidth);
  useEffect(() => {
    const handleResize = () => setWidth(window.innerWidth);
    window.addEventListener('resize', handleResize);
    return () => window.removeEventListener('resize', handleResize);
  }, []);
  return width;
}

What will useWindowWidth() return when used inside a component?
medium
A. The height of the browser window
B. An error because window is undefined
C. The current width of the browser window, updating on resize
D. A function to resize the window

Solution

  1. Step 1: Analyze the hook's state and effect

    The hook uses state to store window.innerWidth and updates it on resize events.
  2. Step 2: Understand the returned value

    The hook returns the current width, which updates whenever the window resizes.
  3. Final Answer:

    The current width of the browser window, updating on resize -> Option C
  4. Quick Check:

    Custom hook returns updated window width [OK]
Hint: Custom hooks return values or functions, not errors here [OK]
Common Mistakes:
  • Confusing width with height
  • Thinking it returns a function
  • Assuming window is undefined in browser
4. What is wrong with this custom hook code?
import { useState, useEffect } from 'react';

function useCounter() {
  const [count, setCount] = useState(0);
  useEffect(() => {
    setCount(count + 1);
  });
  return count;
}
medium
A. It causes an infinite loop because setCount is called inside useEffect without dependencies
B. It returns a function instead of a value
C. It will never update count because useEffect has an empty dependency array
D. It uses useState incorrectly by initializing with 0

Solution

  1. Step 1: Check useEffect dependencies

    The effect has no dependency array.
  2. Step 2: Understand setCount behavior

    setCount(count + 1) runs on every render since there are no dependencies, causing an infinite loop of re-renders.
  3. Final Answer:

    It causes an infinite loop because setCount is called inside useEffect without dependencies -> Option A
  4. Quick Check:

    Missing deps in useEffect = infinite loop [OK]
Hint: useEffect without deps array runs every render [OK]
Common Mistakes:
  • Thinking the effect runs only once without deps array
  • Confusing return value type
  • Misunderstanding dependency array role
5. You want to create a custom hook useFetch that fetches data from a URL and returns { data, loading, error }. Which approach correctly handles loading state and errors inside the hook?
hard
A. Use useState for data, loading, error; use useEffect to fetch and update states with try/catch inside async function
B. Use only useState for data and ignore loading and error states
C. Fetch data directly in the component and pass it to the hook as a parameter
D. Use useEffect without useState and return data from fetch promise

Solution

  1. Step 1: Identify state needs for fetch

    Fetching data requires tracking data, loading status, and errors, so useState is needed for all three.
  2. Step 2: Use useEffect with async fetch and error handling

    Inside useEffect, define an async function with try/catch to fetch data and update states accordingly.
  3. Final Answer:

    Use useState for data, loading, error; use useEffect to fetch and update states with try/catch inside async function -> Option A
  4. Quick Check:

    Fetch with useState and useEffect + try/catch [OK]
Hint: Manage data, loading, error with useState and useEffect [OK]
Common Mistakes:
  • Ignoring loading or error states
  • Fetching outside useEffect causing infinite loops
  • Returning unresolved promises
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard