Skip to content

Instantly share code, notes, and snippets.

@afidegnum
Last active October 11, 2021 16:32
Show Gist options
  • Save afidegnum/c64ede3b52301a538c25843342922cce to your computer and use it in GitHub Desktop.
Save afidegnum/c64ede3b52301a538c25843342922cce to your computer and use it in GitHub Desktop.
lifetime issue
// pub mod comps;
// use comps::hello::*;
use sycamore::prelude::*;
use sycamore_router::{HistoryIntegration, Route, Router, RouterProps};
use wasm_bindgen_futures::*;
#[derive(Route)]
enum Routes {
#[to("/")]
Index,
#[to("/page")]
Page,
#[to("/other_page")]
OtherPage,
#[not_found]
NotFound,
}
#[component(Hello<G>)]
fn hello() -> Template<G> {
let name = Signal::new(String::new());
let name2 = name.clone();
let newinput = name.set(name.get().to_string());
template! {
div {
h1 {
"Hello "
(if *create_selector(cloned!((name) => move || !name.get().is_empty())).get() {
cloned!((name) => template! {
span { (name.get()) }
})
} else {
template! { span { "World" } }
})
"!"
}
input(bind:value=name2)
}
}
}
#[component(App<G>)]
fn app() -> Template<G> {
let name = Signal::new(String::new());
let name2 = name.clone();
template! {
Router(RouterProps::new(HistoryIntegration::new(), |route: StateHandle<Routes>| {
let template = Signal::new(Template::empty());
create_effect(cloned!((template) => move || {
let route = route.get();
spawn_local(cloned!((template) => async move {
let t = match route.as_ref() {
Routes::Index => template! {
h1() { "Index" }
},
Routes::Page => template! {
Hello()
},
Routes::OtherPage => template! {
div {
h1 {
"Hello "
(if *create_selector(cloned!((name) => move || !name.get().is_empty())).get() {
cloned!((name) => template! {
span { (name.get()) }
})
} else {
template! { span { "World" } }
})
"!"
}
input(bind:value=name2)
}
},
_ => template! {
h1() { "Not found" }
},
};
template.set(t);
}));
}));
template! {
div() {
div() {
a(href="/") { "Index" }
br()
a(href="/page") { "Page" }
br()
a(href="/other_page") { "Other page" }
}
(template.get().as_ref().clone())
}
}
}))
}
}
fn main() {
#[cfg(debug_assertions)]
console_error_panic_hook::set_once();
console_log::init().expect("error initializing logger");
sycamore::render(|| {
template! {
App()
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment