dioxus-shareables-1.1.1 has been yanked.
Dioxus Shareables
dioxus-shareables adds hooks for sharing structures between dioxus components. Version 1.0.x provides three interfaces:
-
The shareable!() macro creates a single shared value.
use dioxus::prelude::*;
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(); }
#[allow(non_snake_case)]
pub fn Writer(cx: Scope) -> Element {
let w1 = Var.use_w(&cx); }
-
A List provides an array of shared values.
Using a List<T> rather than a Vec<T> allows components which use only one or two list items to get updated only when the specific list items they use are changed.
use dioxus::prelude::*;
use dioxus_shareables::{shareable, List, ListEntry};
shareable!(Numbers: List<usize> = [3, 5, 7].into_iter().collect());
#[allow(non_snake_case)]
fn IterateOverNumbers(cx: Scope) -> Element {
let nums = Numbers.use_rw(&cx); let w = nums.clone();
cx.render(rsx! {
ul {
nums.read().iter().map(|n| rsx! { ReadANumber { num: n } })
}
})
}
#[allow(non_snake_case)]
#[inline_props]
fn ReadANumber(cx: Scope, num: ListEntry<usize>) -> Element {
let num = num.use_rw(&cx); ...
}
List is a Vec internally, and the methods it implements therefore get their names and behavior from Vec.
-
The shareable_struct!{} macro provides a shared struct with interfaces that encapsulate different behavior.
The idea is that each field of the struct will be stored in a separate global, and loaded only when requested. The actions block describes possible ways of using the struct in terms of what type of access (W or RW) they need to fields of the struct.
The struct can then be initialized using an "action" which describes which fields we need which type of access to.
use dioxus::prelude::*;
dioxus_shareables::shareable_struct! {
pub struct Fuzzy {
wuzzy: u8 = 17,
was_a: u16 = 59,
was_he: &'static str = "bear?",
}
actions for Puzzle {
pub WAS: pub WasTrait = W[was_a, was_he]; INIT = W[wuzzy, was_a], RW[was_he]; }
};
impl<A: FuzzyActions> Fuzzy<A> {
pub fn method(&self) where A: WasTrait {
let me = self.with_actions(WAS); *me.was_he().write() = "bare!"; }
}
fn component(cx: Scope) -> Element {
let fuzzy = Fuzzy::use_(&cx, INIT); fuzzy.method(); }