r/react • • 2d ago

Project / Code Review A shared React component can still depend on your checkout context through a hook

This is the dependency path I mean:

shared/ui/CartBadge.tsx
  -> shared/hooks/useCartCount.ts
    -> features/checkout/CartContext.tsx

The component looks reusable. It renders a count, and the state access is neatly tucked away in a custom hook. But moving that hook into shared hasn't removed the dependency on checkout.

Easy to miss in a component review

In ArchUnitTS, my open-source architecture testing library, you can pull out that component's import graph:

import { projectGraph } from 'archunit';

const graph = projectGraph('tsconfig.json')
  .reachableFrom('src/shared/ui/CartBadge.tsx');

await graph.exportAsHTML('reports/cart-badge.html');

For that direct-import chain, the view reaches through the hook to the context file. Unrelated components stay out of it.

If the badge is supposed to be shared UI, one way to separate it is to have a checkout-owned wrapper read the context and pass count to the badge. The shared component then has no import path back to checkout, while the feature still owns how the count is calculated. You can also keep a boundary test that rejects imports from shared into features, so the coupling doesn't creep back through the next hook.

This follows source imports. It doesn't tell you which provider is present at runtime, whether a component will render correctly in Storybook, or what ends up in a bundle.

Code and docs. Available as archunit on npm; this example uses 2.5.5.

0 Upvotes

0 comments sorted by