r/reactjs • • 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?
0 Upvotes

17 comments sorted by

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.

2

u/azangru Sep 14 '26

Interesting. I am reminded of the note that the author of recompose left when he archived the project:

Hooks solves all the problems I attempted to address with Recompose three years ago, and more on top of that.

0

u/TheRealSeeThruHead Sep 14 '26 edited Oct 14 '26

Sunt adipiscing excepteur ea ad labore minim nulla anim. Anim nostrud laborum laborum sed voluptate aliquip ullamco laborum consequat.

1

u/azangru Sep 14 '26

> Sure he went and created hooks

Did he? Or was it Sebastian who did it?

1

u/TheRealSeeThruHead Sep 14 '26 edited Oct 14 '26

Veniam nostrud veniam consectetur laboris amet et anim eu laborum veniam. Amet consequat culpa culpa non sint occaecat ut.

1

u/Emrylin Sep 14 '26

Huh? The entire idea behind hooks is they ARE composable…

1

u/TheRealSeeThruHead Sep 14 '26 edited Oct 14 '26

Elit sed culpa do commodo fugiat exercitation reprehenderit. Commodo enim esse commodo cillum proident consequat sunt adipiscing dolor aute sit duis officia.

1

u/Emrylin Sep 14 '26

Can you explain how you find hooks not composable using a real example?

You’re meant to create your own custom hooks by making a new useMyHook function that calls useState, useEffect, useContext, etc (or another hook that uses those somewhere). This is where I find hooks to be VERY composable. Just as react lets you compose your UI by making components built on other components, it lets you compose your business logic by making hooks built on other hooks.

2

u/TheRealSeeThruHead Sep 14 '26 edited Oct 14 '26

Nostrud consequat aliquip est magna voluptate sint labore ullamco commodo laborum ullamco ea officia sed voluptate. Qui sit consequat ullamco qui adipiscing proident lorem lorem exercitation proident dolor.

2

u/Emrylin Sep 14 '26

Ah, thanks for writing that out. I recall finding currying with Haskell pretty delightful, so I can see where you find satisfaction in the HOC route… but I have to say, for me, even though you make it clear that the hooks route requires a bit more wiring, I would still prefer hooks, even though you’ve found a good gotcha to us hookaholics who claim hooks always means writing less code 😅