sycamore-rs / sycamore

A library for creating reactive web apps in Rust and WebAssembly
https://sycamore.dev
MIT License
2.88k stars 153 forks source link

Make hooks update in component lifecycle #11

Closed lukechu10 closed 3 years ago

lukechu10 commented 3 years ago

Currently, create_signal and the likes return Rcs. The user must clone them into which ever scope they are used, which is veyr unergonomic. These hooks should create state with the same lifetime as the enclosing component.

Unfortunately, this also means that the simplicity of a single function is no longer possible for defining new components. A proc-macro-attribute could potentially help with this.

lukechu10 commented 3 years ago

Temporary workaround in #34

lukechu10 commented 3 years ago

The way this would work would probably use slab or slotmap so that the keys are Copy. Lifetimes might be tricky to get right but the idea should be that all state created using Signal::new should have the same lifetime and destroyed with component. Passing state up the component tree should be done using props.

If we get the lifetime annotations right, it should be guaranteed by the compiler that Signals are never passed up into a component with a longer lifetime. If that is not possible, runtime checks will have to suffice as it should be a relatively rare scenario.