0
0
Reactframework~8 mins

Reusing logic with hooks in React - Performance & Optimization

Choose your learning style9 modes available
Performance: Reusing logic with hooks
MEDIUM IMPACT
This affects interaction responsiveness and rendering efficiency by reducing unnecessary component updates and re-renders.
Sharing stateful logic across multiple components
React
function useCounter() {
  const [count, setCount] = React.useState(0);
  const increment = React.useCallback(() => setCount(c => c + 1), []);
  return { count, increment };
}

function ComponentA() {
  const { count, increment } = useCounter();
  return <button onClick={increment}>Count: {count}</button>;
}

function ComponentB() {
  const { count, increment } = useCounter();
  return <button onClick={increment}>Count: {count}</button>;
}
Extracting logic into a custom hook avoids code duplication and ensures consistent state management with memoized callbacks.
📈 Performance GainSaves bundle size and improves INP by reducing unnecessary re-renders and re-creating functions.
Sharing stateful logic across multiple components
React
function ComponentA() {
  const [count, setCount] = React.useState(0);
  const increment = () => setCount(count + 1);
  return <button onClick={increment}>Count: {count}</button>;
}

function ComponentB() {
  const [count, setCount] = React.useState(0);
  const increment = () => setCount(count + 1);
  return <button onClick={increment}>Count: {count}</button>;
}
Duplicating state logic in multiple components causes repeated code and separate state management, increasing bundle size and potential bugs.
📉 Performance CostAdds extra bundle size and causes independent re-renders for each component.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Duplicated state logic in componentsMultiple independent statesMultiple reflows per component updateHigher paint cost due to repeated renders[X] Bad
Custom hook with memoized callbacksSingle state logic reusedSingle reflow per state updateLower paint cost due to fewer renders[OK] Good
Rendering Pipeline
Custom hooks encapsulate state and logic, so React only re-renders components when state changes. Memoizing callbacks reduces layout recalculations and paint.
JavaScript Execution
Reconciliation
Layout
Paint
⚠️ BottleneckReconciliation stage due to unnecessary re-renders if hooks are not optimized
Core Web Vital Affected
INP
This affects interaction responsiveness and rendering efficiency by reducing unnecessary component updates and re-renders.
Optimization Tips
1Extract reusable logic into custom hooks to avoid code duplication.
2Use React.useCallback inside hooks to memoize functions and prevent unnecessary re-renders.
3Monitor component re-renders with React DevTools Profiler to ensure hooks improve performance.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance benefit of using custom hooks to reuse logic in React?
ATriggers more layout recalculations
BIncreases bundle size by adding more code
CReduces code duplication and prevents unnecessary re-renders
DBlocks rendering until all hooks finish
DevTools: React DevTools and Performance panel
How to check: Open React DevTools Profiler, record interactions, and observe component re-render counts and durations. Use Performance panel to check layout and paint times.
What to look for: Look for fewer component re-renders and shorter interaction times indicating better INP and less layout thrashing.