Module shared

Module shared 

Source
Expand description

Module shared - Shared values.

The easiest way to add a shared value to your dioxus app is to use the sharable! macro:

NOTE: The type of the shared data must be Send + Sync.

use dioxus_shareables::shareable;

shareable!(Var: usize = 900);

#[allow(non_snake_case)]
pub fn Reader(cx: Scope) -> Element {
    let r = *Var.use_rw(&cx).read(); // this component will update when Var changes.
    cx.render(rsx! {
        "The number is: {r}"
    })
}

#[allow(non_snake_case)]
pub fn Writer(cx: Scope) -> Element {
    let w1 = Var.use_w(&cx); // this component writes to Var, but does not get updated when Var
                             // changes
    let w2 = w1.clone();
    cx.render(rsx! {
        button {
            onclick: move |_| { *w1.write() += 1; },
            "+"
        }
        button {
            onclick: move |_| { *w2.write() -= 1; },
            "-"
        }
    })
}

Structsยง

Shareable
The storage type for a shared global.
Shared
A hook to a shared_value.