When Nested Custom Hooks Get Hard to Follow in React

How layers of nested custom React hooks can hide dependency cascades and extra re-renders, and what helped when I untangled one checkout flow.

· 3 min read · Erik Kubica

In 2014, messy code was easy to spot.

If a jQuery or Backbone codebase was poorly designed, it looked like a 1,200-line file. You opened main.js, saw forty nested callbacks and event listeners mutating DOM nodes, and knew you were dealing with technical debt.

In modern React codebases, the mess can look tidier on the surface. It often hides inside custom hooks.

You open a clean, 25-line component and it looks great:

export function CheckoutModal() {
  const { cart, submitPayment, isProcessing } = useCheckoutFlow();
  return <PaymentForm cart={cart} onSubmit={submitPayment} loading={isProcessing} />;
}

It looks fine. But then a QA engineer reports that typing a single character into the coupon input makes the checkout dialog freeze for 120 ms.

You open useCheckoutFlow() to investigate, and you start following the chain of hooks.

A Six-Level Dependency Tower

Inside useCheckoutFlow(), it turns out there is no simple logic. It delegates to other custom hooks:

// The nested hook pattern
export function useCheckoutFlow() {
  const { user } = useAuth();
  const { discounts } = usePromotions(user);
  const { shipping } = useShippingRates(user?.address);
  const { paymentState, processStripe } = usePaymentGateway(shipping, discounts);
  // ... 40 more lines of useEffect synchronization
}

Each of those sub-hooks contains its own useState, three useEffect hooks, and internal memoized handlers.

When you trace usePaymentGateway(), you find that it calls useCurrencyConverter(), which calls useExchangeRates(), which calls useWebSocketFeed().

That is a six-level tower of nested reactive state.

Hooks re-execute on every parent render, so a small state change in useWebSocketFeed can trigger a re-render cascade through the hooks above it.

In that checkout, React ended up doing fourteen redundant component renders per keystroke:

[Render 1] useAuth updated
[Render 2] usePromotions invalidated by user object identity change
[Render 3] useShippingRates re-evaluates shipping costs
[Render 4] usePaymentGateway recalculates order total
[Render 5] CheckoutModal re-renders children

When a dependency cycle forms between two hooks listening to each other's state changes, you can also end up with Maximum update depth exceeded in the console.

A custom hook is a nice way to share logic, but it does not fix unclear boundaries by itself.

Flattening the Structure

When I refactor this kind of code, I try to follow three rules:

  1. Extract pure domain logic: If a calculation does not touch the DOM or React lifecycle, it does not need to be in a custom hook. It can be a plain TypeScript function (calculateTax(items, rate)) that can be tested without a browser environment.
  2. Keep hook nesting shallow, about two levels: I try not to have a custom hook call another custom hook unless it is a well-known primitive (useQuery or useForm). It is a rule of thumb, not a law.
  3. Lift shared state to explicit context: If four components need user permissions, I provide it via a top-level context provider rather than creating five independent reactive listener hooks.

What Changed on That Project

After untangling a nested hook structure in a SaaS checkout flow:

  • Browser Heap Memory: Dropped from 68 MB down to 14 MB RAM, after removing runaway closure allocations.
  • Main Thread CPU Utilization: Fell by about 85%, and animations ran smoothly at 60 fps again.
  • Input Latency: Dropped from 120 ms typing lag to about 2 ms.
  • Code Traceability: The files needed to trace a payment transaction went from 8 nested hook files to 2 readable modules.

Those numbers are from that one project and will differ elsewhere. Custom hooks are a good tool for encapsulating stateful browser APIs. Where I ran into trouble was using them to hide domain complexity, which made the state harder to follow.

Tell me what you need built

Usually a reply within a day, by email or WhatsApp.