signals icon indicating copy to clipboard operation
signals copied to clipboard

feat: add `jsxBind` function to @preact/signals

Open aleclarson opened this issue 5 months ago • 7 comments

Background

I briefly discussed this idea with @rschristian, who said I should try to implement it.

Link to discussion: https://preact.slack.com/archives/C3NMBSJKH/p1752172452437339

Goal

Avoid needing to declare "computed expressions" with useComputed, enabling you to write "inline computeds" where they're needed. This is intended for computeds that are only used by one JSX attribute or JSX child.

How It Works

It's as easy as wrapping a "computed callback" with jsxBind(…), a function exported by the @preact/signals package.

Key Features

  • Host elements only: Only host elements support this feature
  • No rerender updates: Just like useComputed, the callback does not update on rerender
  • Dual support: This implementation supports use in JSX attributes and JSX children

Current Limitations

Currently, there is no warning when trying to use jsxBind with a composite element, as this would require iterating the props object. Maybe someone has an idea about how to efficiently check for such misuse.

Naming

Originally, I named it bind(), but decided jsxBind() makes its purpose a bit clearer. Ultimately, the name is up to you guys. :)

aleclarson avatar Jul 11 '25 22:07 aleclarson