r/reactjs • u/RadioLast5093 • 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
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.
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/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/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/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:
onSomethingthreaded through 3 layout components is the one I see most often in agent-written code, and it's exactly wherechildren/ slot props would have avoided it. If you can only report one kind, I'd report those.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
...propsas "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.
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.