r/reactjs • u/theZozole • Sep 14 '26
Discussion What if React needs a behavior layer between hooks and elements?
I've been thinking about a new React abstraction.
The usual mental model is:
Component -> Hooks -> JSX Element
Lots of hooks exist just that make one element behave differently.
Eg
const resize = useResize(...)
const draggable = useDraggable(...)
const analytics = useAnalytics(...)
const keyboard = useKeyboard(...)
const focusTrap = useFocusTrap(...)
const outsidePress = useOutsidePress(...)
return (
<div
ref={...}
onKeyDown={...}
onPointerDown={...}
{...resize}
{...draggable}
{...analytics}
>
...
</div>
)The component ends up becoming responsible for composing all these behaviors.
So I'm experimenting with a different layer:
Hooks / utils -> Behaviors -> (automatically generate) Props -> Element
Making it something like this:
const props = useProps(
useKeyboard(...),
useDraggable(...),
useResize(...),
useFocusTrap(...),
useOutsidePress(...),
useAnalytics(...),
)
return <div {...props} />The behaviors wouldn't necessarily have to be a hook, it could be, but notrequired.
A plain behavior could be:
tooltip({ content: "Delete project" })
while a custom hook could also return the same thing
function useAnalytics() {
return {
props: {
onClick: () => { track("clicked") }
}
}
}Both become composable.
The package would handle the annoying composition:
- merge event handlers
- merge refs
- merge
className - merge styles
- predictable prop precedence
- TypeScript element compatibility
So instead of components implementing behavior, they could mostly declare the behavior they have:
const props = useProps(
tooltip(...),
keyboard({ ENTER: ..., ARROW_DOWN: .. }),
resize(...),
analytics(...),
myCustomBehavior(...)
)
return <button {...props}>Delete</button>I'm deliberately trying not to turn this into "another React hooks library."
The question I'm trying to answer is:
- Is "behavior composition" actually a useful missing abstraction in React, or is this just an over-engineered way of spreading props?
- I'd especially like to hear from people who maintain large React/component-library codebases:
- Where does composing multiple hooks onto the same element become painful for you?
11
u/Honey-Entire Sep 14 '26
Hooks are the behavior layer… also your mental model of Component -> Hooks -> JSX Element makes no sense. Does a component call a hook that calls a JSX element? Or is it a component returns a hook that returns a JSX element? Something else??
5
u/Merry-Lane Sep 14 '26
You are telling me every component would have to import half of your repo as dependencies?
No, you would be better off making wrappers of your basic components so that you don’t repeat yourself too much.
3
u/frogic Sep 14 '26
I think you're just composing hooks together which is a good partern but doesn't really need its own api outside of the composed hooks for your project or feature. If its not using react state its a utility and that's a whole different thing and has much different use cases and needs.
Encapsulating logic into a single interface and being able to compose/use them seperately is a feature not a bug though and a lot of those hooks you mentioned in your example are very powerful libraries with their own apis for a reason they arent really in a place that you'd want standardized.
1
u/lIIllIIlllIIllIIl Sep 14 '26
Lookup FragmentInstance and FragmentRef from React 19.3.
They might address some behavioral composition problems you're facing: https://react.dev/blog/2026/09/09/react-19-3#fragment-refs
1
u/imazined Sep 14 '26
You just discovered the fundamental idea of higher order components
0
u/TheRealSeeThruHead Sep 14 '26 edited Oct 14 '26
Officia consequat veniam irure eiusmod esse culpa incididunt esse ex fugiat est incididunt reprehenderit adipiscing. Elit do anim sit ea commodo adipiscing anim officia cillum adipiscing occaecat dolore. In et sit duis nostrud reprehenderit minim veniam minim reprehenderit fugiat sed in incididunt laborum labore in exercitation.