Created
October 12, 2021 10:59
-
-
Save afidegnum/d8d52d3b05bb3434d13849a9b71a2052 to your computer and use it in GitHub Desktop.
values not fetched from routes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pub mod comps; | |
// use comps::hello::*; | |
use sycamore::context::{use_context, ContextProvider, ContextProviderProps}; | |
use sycamore::prelude::*; | |
use sycamore_router::{HistoryIntegration, Route, Router, RouterProps}; | |
use wasm_bindgen_futures::*; | |
#[derive(Clone)] | |
struct InputTxt(Signal<String>); | |
#[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(); | |
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> { | |
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! { | |
h1() {"Other Pages"} | |
}, | |
_ => 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