# Dioxus Shareables
`dioxus-shareables` adds hooks for sharing structures between `dioxus` components. Version 0.2.x provides three interfaces:
1. The `shareable!()` macro creates a single shared value.
```rust
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); }
```
1. 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.
```rust
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`.
1. 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.
```rust
use dioxus::prelude::*;
dioxus_shareables::shareable_struct! {
pub struct Fuzzy {
wuzzy: u8 = 17,
was_a: u16 = 59,
was_he: &'static str = "bear?",
}
action WAS impl pub WasTrait = W[was_a, was_he]; action 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(); cx.render(rsx!{div{}})
}
```
## A note on version numbers.
If you're looking at the versions on Crates.io, you might notice that there the first release was a version 1.0. The simple version of the story is that I'm new to sharing my code with others and did *not* think about semver. Since then I've yanked all the release versions to sync the version numbers on this crate with the latest dioxus prerelease version.