Ambient icon indicating copy to clipboard operation
Ambient copied to clipboard

`Copy` setters struct for hooks (instead of arc closure)

Open FredrikNoren opened this issue 2 years ago • 4 comments

The UI code has these "clone blocks" everywhere right now:{ let set_some = set_some.clone(); move |_| set_some("test")}.

Leptos has solved this by instead of returning a closure from use_state (or create_signal in their case), they return a Copy struct, which looks like this: https://docs.rs/leptos_reactive/0.2.5/src/leptos_reactive/signal.rs.html#827

Would be interesting to explore if we could do something similar.

FredrikNoren avatar Apr 05 '23 18:04 FredrikNoren

👋 We built a similar system for Dioxus and published it as a separate library: generational-box is similar to Leptos' copy sync system except:

  • You can include your own reactivity system or not
  • You can call the value like a function on stable
  • You can read and write to the value directly instead of using with_ methods

ealmloff avatar Dec 07 '23 00:12 ealmloff

@ealmloff Oh that looks awesome! Thanks for letting us know

FredrikNoren avatar Dec 10 '23 11:12 FredrikNoren

@ealmloff Btw are there any plans for making EventHandler etc. use that? Would be ⭐amazing⭐ if it was possible to just move that around more freely

FredrikNoren avatar Dec 15 '23 14:12 FredrikNoren

@ealmloff Btw are there any plans for making EventHandler etc. use that? Would be ⭐amazing⭐ if it was possible to just move that around more freely

That is something we have been discussing. We could make this version of event handlers copy. The issue is we would need to make the event handles 'static instead of using the bump lifetime which means you need to clone all non-signal hooks which would be very breaking. (It is the same as the issue discussed here https://discord.com/channels/899851952891002890/928812591126569000/1181770301827207258)

ealmloff avatar Dec 15 '23 15:12 ealmloff