DEV Community

Daniel Pertu
Daniel Pertu

Posted on

1,029 props across 490 React components, 144 of them className, and only 55 callbacks in the entire app

How many props does a component take? There are answers to that in style guides, and there is an answer in your repository, and they are not the same answer. I counted the real one for Nakodo, a Next 16 app of 490 React components, by parsing the destructuring pattern of every one of them.

The count that surprised me is at the bottom, and it is not about props at all.

The shape

490 components   (function Name(...) or const Name = (...) => in a .tsx file)
 72 take no props at all
396 destructure an object
 22 take a single named parameter without destructuring

1,029 prop sites
  328 distinct prop names
  182 of those appear exactly once
Enter fullscreen mode Exit fullscreen mode

Median props per component: 2. Maximum: 16, in a brief editor that is overdue for a split, and everything above 10 is in the same three editor screens.

Where they live:

Components Prop sites ...props spreads
app/ (routes and screens) 279 655 2
components/ (our own shared) 103 218 0
components/ui/ (vendored) 108 156 105

That last column is the clearest line in the whole census. The ...props rest spread, the pattern where a component forwards every attribute it does not recognise, appears 107 times, and 105 of those are in vendored component code I did not write. In my own 382 components it appears twice.

It is not a rule I set. It is what happens when every component in a codebase is written for exactly one place: there is no unknown caller to forward attributes on behalf of, so nobody writes the forwarding.

The top of the vocabulary

Prop Uses
className 144
children 63
label 31
campaignId 30
title 26
kind 24
params 23
onChange 18
value 16
size 14
id 12
platforms 12
email 11
variant 11
searchParams 11

className is 14 per cent of every prop in the codebase, and more than four times the next entry. That is the tax on composing with utility classes: a component that might be placed in two layouts has to accept a margin from its parent, so it takes a className, merges it, and the prop appears in 144 signatures.

It is a real cost and I do not know a better trade. The alternative is a component that owns its own spacing, which is correct until the second call site, and then becomes a compact boolean, and we have four of those too.

Two entries in that list are not mine: params and searchParams are Next's page props, so 34 of the 1,029 are the framework's vocabulary rather than ours.

campaignId at 30 is the honest description of what this application is. Thirty components need to know which campaign they are looking at, which means a campaign id is the unit of work in the UI the same way it is in the database, and any refactor that changed its shape would touch thirty signatures. That is the sort of thing you want to know before you decide to change it, and it is not visible from any file.

328 names, and 182 of them used once

The long tail is more than half the vocabulary. 182 prop names appear in exactly one component, which means they are not vocabulary at all, they are arguments: onAskTyped, onCustomerValue, perDay, hatchId.

I spent a while deciding whether that is bad, and I do not think it is. A shared prop name is an interface, and an interface is only worth having when two things implement it. Naming a one-off argument after the thing it is, in the one component that takes it, is cheaper than inventing a general name and pretending.

The place it does bite is consistency at the top of the distribution. We have label 31 times and title 26 times and text 7 times, and I cannot give you a rule that distinguishes them. That is three words doing one job, in a codebase that has already been caught using several words for the same concept in its public copy. The fix is not a lint rule, it is picking one and doing a pass.

Only 63 of 490 take children

63 components accept `children`
Enter fullscreen mode Exit fullscreen mode

That is 12.9 per cent, and it says this codebase is almost entirely leaves. A component that takes children is a container: a layout, a card, a panel, a provider. A component that does not is a thing that takes data and returns markup.

The ratio tells you how composition happens here. Nearly every screen is a route file that imports twenty specific components and arranges them, rather than a tree of generic containers configured by props. That is more code at the top and less indirection in the middle, and it is why params and className are near the top of the list while nothing like render or as appears at all.

The number that describes the architecture

24 distinct callback props  (names matching /^on[A-Z]/)
55 uses of them, across 490 components
18 of those 55 are onChange
Enter fullscreen mode Exit fullscreen mode

Fifty-five callbacks. In a React application with 490 components.

For most of the history of React, the callback count would have been in the hundreds, because the way anything happened was a child telling a parent that something happened, up through however many layers, to whoever held the state and could do something about it.

Here, almost nothing travels upward. 80 of our 582 source files carry "use client", and the rest run on the server, so the overwhelming majority of these 490 components are rendered once, on the server, from data a route already fetched. When a user does something that changes data, it goes to a Server Action, which is a POST to the page's own URL, and the answer is a re-render from the server. There is no parent to notify. The 55 callbacks that do exist are concentrated exactly where you would expect: the three editor screens, a search field, the support panel.

The public pages make the same point from the other end. The only genuinely interactive things on the marketing site are four calculators, and the components behind them look like this:

export function InstagramEngagementRateCalculator() { /* ... */ }

function PerViewCalculator({ platform }: { platform: keyof typeof PER_VIEW_COPY }) { /* ... */ }
Enter fullscreen mode Exit fullscreen mode

Zero props and one prop. Open the engagement rate calculator or the sponsorship cost calculator, type a number, and watch the network tab: nothing leaves the page, nothing is lifted, and no parent is told. The state is inside the component that owns the question, and the arithmetic is beside it.

I would not have predicted 55 before I counted, and I would have guessed four hundred. It is the single best measurement I have taken of what moving rendering to the server actually did to the code, and it took one regex over one directory.

What I am changing

  • Pick one of label, title and text for a short piece of visible text, and do the pass. 64 sites, one afternoon.
  • Split the 16-prop editor. A component with 16 props is two components and a shared type, and in this case the seam is obvious from the names.
  • Leave the 182 single-use names alone, and stop feeling bad about them.
  • Leave className where it is, with the note that 144 is the price of the layout model, not a smell to be cleaned up.

If you want to run it on your own codebase, parse the destructuring pattern rather than the type, because the type is often ComponentProps<"div"> & {...} and tells you nothing about what the component actually reads. And count your callbacks. The number is a fact about your architecture that no amount of reading the files will give you.

Top comments (0)