leptos-use/examples/ssr/src/app.rs

74 lines
2.2 KiB
Rust
Raw Normal View History

2023-07-14 22:43:19 +01:00
use crate::error_template::{AppError, ErrorTemplate};
use leptos::ev::{keypress, KeyboardEvent};
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use leptos_use::storage::use_local_storage;
use leptos_use::{
use_debounce_fn, use_event_listener, use_intl_number_format, use_window,
UseIntlNumberFormatOptions,
2023-07-14 22:43:19 +01:00
};
#[component]
2023-07-27 18:06:36 +01:00
pub fn App() -> impl IntoView {
2023-07-14 22:43:19 +01:00
// Provides context that manages stylesheets, titles, meta tags, etc.
2023-07-27 18:06:36 +01:00
provide_meta_context();
2023-07-14 22:43:19 +01:00
view! {
<Stylesheet id="leptos" href="/pkg/start-axum.css"/>
<Title text="Leptos-Use SSR Example"/>
2023-07-27 18:06:36 +01:00
<Router fallback=|| {
2023-07-14 22:43:19 +01:00
let mut outside_errors = Errors::default();
outside_errors.insert_with_default_key(AppError::NotFound);
2023-07-27 19:48:21 +01:00
view! { <ErrorTemplate outside_errors/> }.into_view()
2023-07-14 22:43:19 +01:00
}>
<main>
<Routes>
2023-07-27 18:06:36 +01:00
<Route path="" view=|| view! { <HomePage/> }/>
2023-07-14 22:43:19 +01:00
</Routes>
</main>
</Router>
}
}
/// Renders the home page of your application.
#[component]
2023-07-27 18:06:36 +01:00
fn HomePage() -> impl IntoView {
2023-07-14 22:43:19 +01:00
// Creates a reactive value to update the button
2023-07-27 18:06:36 +01:00
let (count, set_count, _) = use_local_storage("count-state", 0);
2023-07-14 22:43:19 +01:00
let on_click = move |_| set_count.update(|count| *count += 1);
let nf = use_intl_number_format(
UseIntlNumberFormatOptions::default().locale("zh-Hans-CN-u-nu-hanidec"),
);
2023-07-27 18:06:36 +01:00
let zh_count = nf.format::<i32>(count);
2023-07-14 22:43:19 +01:00
2023-07-27 18:06:36 +01:00
let (key, set_key) = create_signal("".to_string());
2023-07-14 22:43:19 +01:00
// window() doesn't work on the server so we provide use_window()
let _ = use_event_listener(use_window(), keypress, move |evt: KeyboardEvent| {
set_key(evt.key())
2023-07-14 22:43:19 +01:00
});
2023-07-27 18:06:36 +01:00
let (debounce_value, set_debounce_value) = create_signal("not called");
2023-07-14 22:43:19 +01:00
let debounced_fn = use_debounce_fn(
move || {
set_debounce_value("called");
},
2000.0,
);
debounced_fn();
2023-07-27 19:48:21 +01:00
view! {
<h1>Leptos-Use SSR Example</h1>
<button on:click=on_click>Click Me: {count}</button>
<p>Locale zh-Hans-CN-u-nu-hanidec: {zh_count}</p>
<p>Press any key: {key}</p>
<p>Debounced called: {debounce_value}</p>
2023-07-14 22:43:19 +01:00
}
}