Attribute Macro perseus::auto_scope

source ·
#[auto_scope]
Expand description

A helper macro for templates that use reactive state. Once, this was needed on all Perseus templates, however, today, templates that take no state, or templates that take unreactive state, can be provided as normal functions to the methods .view() and .view_with_unreactive_state() respectively, on Perseus’ Template type.

In fact, even if you’re using fully reactive state, this macro isn’t even mandated anymore! It just exists to turn function signatures like this

fn my_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a MyStateRx) -> View<G>

into this

#[auto_scope]
fn my_page<G: Html>(cx: Scope, state: &MyStateRx) -> View<G>

In other words, all this does is rewrites some lifetimes for you so Perseus is a little more convenient to use! It’s worth remembering, however, when you use this macro, that the Scope is actually a BoundedScope<'app, 'page>, meaning it is a child scope of the whole app. Your state is a reference with the lifetime 'page, which links to an owned type that the app controls. All this lifetime complexity is needed to make sure Rust understands that all your pages are part of your app, and that, when one of your users goes to a new page, the previous page will be dropped, along with all its artifacts (e.g. any create_effect calls). It also makes it really convenient to use your state, because we can prove to Sycamore that it will live long enough to be interpolated anywhere in your page’s view!.

If you dislike macros, or if you want to make the lifetimes of a page very clear, it’s recommended that you don’t use this macro, and manually write the longer function signatures instead. However, if you like the convenience of it, this macro is here to help!

Note: this can also be used for capsules that take reactive state, it’s not just limited to templates.