* improve documentation for the fullstack server context * Add a section about axum integration to the crate root docs * make serve_dioxus_application accept the cfg builder directly * remove unused server_fn module * improve fullstack config docs * improve documentation for the server function macro * fix axum router extension link * Fix doc tests * Fix launch builder * Simplify the launch builder * don't re-export launch in the prelude * refactor fullstack launch * Fix fullstack launch builder * Update static generation with the new builder api * fix some formatting/overly broad launch replacements * fix custom menu example * fix fullstack/static generation examples * Fix static generation launch * A few small formatting fixes * Fix a few doc tests * implement LaunchConfig for serve configs * fix fullstack launch with separate web and server launch methods * fix check with all features * dont expose inner core module * clippy and check * fix readme --------- Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
61 lines
1.6 KiB
Rust
61 lines
1.6 KiB
Rust
//! Example: Global signals and memos
|
|
//!
|
|
//! This example demonstrates how to use global signals and memos to share state across your app.
|
|
//! Global signals are simply signals that live on the root of your app and are accessible from anywhere. To access a
|
|
//! global signal, simply use its methods like a regular signal. Calls to `read` and `write` will be forwarded to the
|
|
//! signal at the root of your app using the `static`'s address.
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
const STYLE: &str = asset!("./examples/assets/counter.css");
|
|
|
|
static COUNT: GlobalSignal<i32> = Signal::global(|| 0);
|
|
static DOUBLED_COUNT: GlobalMemo<i32> = Memo::global(|| COUNT() * 2);
|
|
|
|
fn main() {
|
|
dioxus::launch(app);
|
|
}
|
|
|
|
fn app() -> Element {
|
|
rsx! {
|
|
head::Link { rel: "stylesheet", href: STYLE }
|
|
Increment {}
|
|
Decrement {}
|
|
Reset {}
|
|
Display {}
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
fn Increment() -> Element {
|
|
rsx! {
|
|
button { onclick: move |_| *COUNT.write() += 1, "Up high!" }
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
fn Decrement() -> Element {
|
|
rsx! {
|
|
button { onclick: move |_| *COUNT.write() -= 1, "Down low!" }
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
fn Display() -> Element {
|
|
rsx! {
|
|
p { "Count: {COUNT}" }
|
|
p { "Doubled: {DOUBLED_COUNT}" }
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
fn Reset() -> Element {
|
|
// Not all write methods are available on global signals since `write` requires a mutable reference. In these cases,
|
|
// We can simply pull out the actual signal using the signal() method.
|
|
let mut as_signal = use_hook(|| COUNT.resolve());
|
|
|
|
rsx! {
|
|
button { onclick: move |_| as_signal.set(0), "Reset" }
|
|
}
|
|
}
|