preactjs / signals

Manage state with style in every framework
https://preactjs.com/blog/introducing-signals/
MIT License
3.71k stars 91 forks source link

Option to use signals-core in React with simple hook #364

Closed user753 closed 1 year ago

user753 commented 1 year ago

I would prefer to use signals in react without __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED shenanigans Something like this

const xSignal = signal(100)

function Foo(){
   const x = useSignal(xSignal)
   return <div>{x}</div>
}

Maybe there is implementation of such hook?

rschristian commented 1 year ago

We don't exactly use __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED because we thought it'd be a laugh.

While you can support the above really easily, the issue comes when you need to trigger a rerender, at which point the API ergonomics and perf improvements go out the window.

The main innovation/selling point of signals is the integration into the view library that gives you much better performance. If we were to discard that, then there's not really any reason to use this over any other state library. This, combined with the React teams' opinion that other state primitives should not be supported, results in a situation in which we have to choose compelling features vs avoidance of __SECRET_INTERNALS. We chose the former.

You might be able to hack something together, but I'm just not sure why you would. Valtio, etc. already exist.

That being said, we'd love to get off using __SECRET_INTERNALS if possible, we just haven't found a method that doesn't create bugs and edge cases.

user753 commented 1 year ago

Thanks for the answer.

but I'm just not sure why you would. Valtio, etc. already exist.

I think jotai would be a better option for comparison. I want to use this package because I believe it's the only popular vanilla js implementations of signals. There are jotai, recoil, angular signals, solid signals, but all on them depend on some view libraries. Do you know by any change other good implementations?

rschristian commented 1 year ago

I think jotai would be a better option for comparison.

Not really? Valtio is a mutable state primitive and close-ish to what we're doing here, while Jotai is very much not. If you like the feel of signals, but not the perf benefit/internals risk, it's probably the closest (mainstream and supported in React) option out there to my knowledge. Jotai is much closer to normal React with the separate getters and setters.

There are jotai, recoil, angular signals, solid signals, but all on them depend on some view libraries.

I think Solid does, I think it provides a separate package of state primitives? Not a Solid user though, I could be wrong. Vue provides their reactivity system as a standalone package, though the documentation is a bit sparse.

Valtio does also support vanilla JS, for what it's worth.

user753 commented 1 year ago

Thanks I will try to use valio.