r/reactjs • • 2d ago

Discussion Is a prop that passes through 3 components that never read it already prop drilling?

I maintain a structure linter and had to turn "prop drilling" into a number, which turned out harder than writing the check.

What I settled on: a prop counts as drilled when 3 or more components in a row only declare it and hand it to a child. Reading it once, even in a condition, takes the component out of the chain. So does binding it to a child's event (`onClick={onSave}`), and so does spreading `...props`.

The case that convinced me the rule is worth having, from a well known open source app: `mountUnavailableModelTooltip` goes ArtifactPanel -> SideTaskPanel -> SideTaskSession -> ChatPane -> ChatEditor -> ToolbarPopover -> ToolbarDropdownRow. Seven components carry it, none of them use it.

To check the threshold I ran it on 105 open source TypeScript projects. 58 had at least one such chain. Out of 881 drilled props, 658 were exactly 3 deep, so the threshold decides almost everything: at 4 the rule would report a quarter of that.

What I did not expect is that context was rarely the fix. Usually the top component could render the bottom one itself and pass it down as `children`, and the middle ones would never hear about the value.

So, people who review React code for a living: is 3 where it starts to bother you, or do you only care at 4+?

The rule is part of unknot, a linter I wrote: https://github.com/PatternVoid/unknot

0 Upvotes

26 comments sorted by

21

u/octocode 2d ago

there’s no minimum count of intermediate components to be considered prop drilling.

technically passing through 1 intermediate component is already drilled.

whether this is considered problematic is up to your own preference and requirements.

0

u/CandidResearch9567 2d ago

passing through a single wrapper or layout component happens so often that flagging 1 intermediate step in a linter would just drown everyone in warnings

0

u/octocode 2d ago

if you prefer a nested structure then probably. my team prefers composition, so any drilling is a red flag.

-1

u/RadioLast5093 2d ago

Fair, by definition one intermediate component is already drilling. I worded the title badly: I'm not after the definition, I'm after the point where a check should start complaining by default.

At 1 it fires on nearly every component tree and people turn it off the same day. So it's really a question about a default: at what depth would you want a reviewer to push back? I went with 3 and made it a setting (`minComponents`) for teams that want it stricter.

What do you use in practice, if anything?

0

u/octocode 2d ago edited 1d ago

to be honest, we don’t really prefer nesting components at all outside of very specific situations. any prop drilling is probably an opportunity to use composition instead of nesting

1

u/xXxdethl0rdxXx 2d ago

3 is tough, and yes, context isn't always the right move. Another option is to just render them nested in a parent component and wrap them instead, rather than 3+ separate component files. That may be totally legit based on the situation.

1

u/RadioLast5093 2d ago

Yes, that's the fix I ended up applying most often: the parent renders the leaf itself and the middle components just take `children`. Nobody in between has to know the value exists.

When you say 3 is tough, do you mean too strict or too lenient?

0

u/xXxdethl0rdxXx 2d ago

I think it’s something I’d flag in a review, but not block if it were an edge case or had an excellent reason.

1

u/horizon_games 2d ago

Man this is some wild React specific stuff. Really don't think we need a separate Claude based git project to diagnose it.

-1

u/RadioLast5093 2d ago

Fair, but that's not what the package is. Prop drilling is one rule out of 13, and it's the only reason I posted in this sub.

The rest has nothing to do with React: folders that import each other, import cycles, classes that know about each other, constructors with 10+ dependencies, forwardRef in NestJS/Angular, the same type declared twice in one package, 1500-line files. It runs on any TS/JS repo, backend included.

It's also not Claude based. It's plain static analysis on the TypeScript compiler API: no model, no API calls, no network, same output every run. npx unknot in CI is the whole setup. The Claude Code part is an optional hook that runs the same check after an agent edits code, because agents are very good at producing exactly this kind of structure.

0

u/horizon_games 1d ago

Given the Git history on your repo we have different meanings of "Claude based"

1

u/worldlymatron 2d ago

Three seems reasonable as a warning but not necessarily an error

1

u/TobiasMcTelson 1d ago

Usually, complex ui or data states that need to sync between far different nodes of three start the prop drilling. If you reach it, maybe think into a simple global state lib. I like jotai because there’s no boilerplate, it’s lightweight and u can usually start to think into atoms as global useState. It’s not the best mental model, but it’s the zero friction. Later you develop better skills with it

1

u/suubu-crtiv 1d ago

3 as a warning feels right, since at 2 it's usually just a layout wrapper. Quick test: if the middle components still make sense with the prop deleted, lift it and pass the leaf down as children.

0

u/Ahmed_Hisham0 2d ago

everyone in this thread is arguing about the definition, which is the wrong axis. what your linter should count is the thing that actually costs you: blast radius.

a prop that passes through 3 components and is read nowhere in between is a tunnel. the cost isn't that it's ugly, it's that when the leaf needs a new field, you edit 4 files. so the useful metric is "how many component signatures change when this data changes", not "how deep is it". depth 2 with 6 siblings all threading the same value is worse than depth 5 on one linear chain.

the read-once rule you settled on is a decent proxy, but i'd tighten one case: a component that only forwards it to an event handler is still a tunnel, and that's the one that hurts most, because the handler usually lives in whatever component actually owns the state. that pattern is what people should fix, not the depth.

if you want a name for the report, "prop tunnels" reads better than drilling and it's more precise about what the linter found.

0

u/RadioLast5093 2d ago

This is the most useful reply in the thread, thanks.

Blast radius is what I actually care about too, depth was just the easy thing to count. The data backs you up: props almost never travel alone. In the 105 projects I ran it on, 881 drilled props start from about 600 components, so the same chain usually carries several values and a change at the leaf touches every signature on the way. Counting "signatures that change together" instead of depth would rank those correctly, and your depth 2 with 6 siblings case is one my current rule misses completely.

On handlers we're closer than it sounds. `onSave={onSave}` handed down under the same name counts as a tunnel. What I let through is a component wiring it to a different event, `<Button onClick={onSave} />`, because that component decides when it fires. But you're right that the handler usually belongs to whoever owns the state, so a long run of those is the same smell. I'll look at how many chains that adds.

"Prop tunnels" is a better name for what gets reported. Might steal it.

0

u/Ahmed_Hisham0 2d ago

the 881 props over 600 components is the number that makes the case — that's basically every chain carrying a second value. so ranking by signatures-changed-together would catch exactly what my rule can't see.

one addition: distance from the state owner probably predicts refactor pain better than depth does. a prop 8 levels deep inside one feature is cheap, a prop 2 levels deep that crosses a feature boundary is where the churn actually lives. if the linter can see module boundaries, that's a cheap extra flag.

on the button case, the line i'd draw is timing vs payload. <Button onClick={onSave} /> changes when it fires, so it's doing something real. same handler, same name, same payload, just relocated into a named component, is still a tunnel even though the tag changed.

steal away. it's a better word for it than drilling.

0

u/codariadev 2d ago

3 feels right as a default to me, mostly because of what you found: the fix is usually composition, not context, and composition gets more annoying to apply the longer the chain gets. At 2 it's often just a layout component and a leaf, and that's fine.

Two things I'd consider before settling on it:

  • Weight callbacks differently from data. A value going 3 levels down is usually a smell, but an onSomething threaded through 3 layout components is the one I see most often in agent-written code, and it's exactly where children / slot props would have avoided it. If you can only report one kind, I'd report those.
  • Report the chain, not each component. Seeing "ArtifactPanel -> ... -> ToolbarDropdownRow carries mountUnavailableModelTooltip, used only in the last one" in a single message is what makes someone actually fix it. Seven warnings, one per file, would get the rule disabled.

Treating ...props as "not drilled" makes sense to avoid false positives, but it's also where the worst chains hide. Maybe a separate, opt-in rule for that?

0

u/WorthRow3022 2d ago

3 bothers me, but not equally. With 658 of your 881 at exactly 3, I'd let the threshold stay at 3 and vary the severity instead: 3 as a hint, 4+ as a warning. Then the long chains like your seven-component tooltip one stand out, and the common case doesn't drown everything else.

What raises it for me more than depth:

- Callbacks more than data. A drilled `onSave` usually means the deep component is reaching back up to do something, which is exactly the case where rendering it at the top and passing it as `children` works.

- Renames. A middle component that only turns `onSave` into `onSubmit` reads it in a technical sense, but it's still plumbing, and it makes the chain harder to follow. I'd keep it in the chain.

- The same prop drilled through several branches. One chain of 3 is fine; the same value through 3 components in five different places is the one I'd want flagged.

-1

u/amayle1 2d ago

It’s a fools errand. If you or others on your team are bothered by it then fix it. If you aren’t then don’t. You don’t need some linter for it.

2

u/Sacredfice 2d ago

This is one hard lesson from a decade of experience. If people complain about the code then tell them fix themselves instead of whining. In the end they won't do it lol

0

u/RadioLast5093 2d ago

For a team that reads every diff, agreed, you notice it and fix it.

My case is different: most of the code I review now is written by agents, and they add one more pass-through prop every time because it's the cheapest edit. No single diff looks wrong. A check is the only thing that says "this chain is 5 deep now" before I find out by accident.

If your team doesn't have that problem, you really don't need it.

2

u/amayle1 2d ago

I’d argue if you are that automated I don’t think prop drilling is even an issue. Would a human ever even need to go through five levels of components?

Cause it’s not some performance issue, or “easy to introduce a bug” issue. It’s just a readability issue. And if you are not reading it, well…

0

u/FahadAppMaster 33m ago

Three feels right to me as a warning. In my own React code, one or two pass-throughs are usually just layout wrappers, but at three I start losing track of where a value actually gets used. I usually fix it by having the parent render the leaf as a child instead of reaching for context first.