pub struct List<T>(_);
Expand description

A list of shareable 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_shareables::{shareable, List, ListEntry};

shareable!(Numbers: List<usize> = [3, 5, 7].into_iter().collect());

#[allow(non_snake_case)]
fn ListNumbers(cx: Scope) -> Element {
    let nums = Numbers.use_rw(&cx); // This component is updated when new items are added to or
                                    // removed from the list, but not when the individual list
                                    // items change.
    let w = nums.clone();
    cx.render(rsx! {
        ul {
            nums.read().iter().map(|n| rsx! { ListItem { num: n } })
        }
        button {
            onclick: move |_| {
                let mut w = w.write();
                let sum = w.iter().map(|n| *n.share().read()).sum();
                w.push(sum)
            },
            "Sum"
        }
    })
}

#[allow(non_snake_case)]
#[inline_props]
fn ListItem(cx: Scope, num: ListEntry<usize>) -> Element {
    let num = num.use_rw(&cx); // This component is updated when this specific entry in the
                               // list is modified.
    let w1 = num.clone();
    let w2 = num.clone();
    let num = num.read();

    cx.render(rsx! {
        li {
            "{num}",
            button { onclick: move |_| *w1.write() += 1, "+" }
            button { onclick: move |_| *w2.write() -= 1, "-" }
        }
    })
}

List is a Vec internally, and the methods it implements therefore get their names and behavior from Vec.

Implementations

See [’Vec::truncate`]

See [’Vec::try_reserve`]

See [’Vec::try_reserve_exact`]

See [’Vec::with_capacity`]

See [[_]::binary_search]

See [[_]::binary_search]

See [[_]::binary_search_by_key]

See [[_]::contains]

See [[_]::ends_with]

See [[_]::fill]

Note: This replaces items, rather than changing their value, so components which were linked to the list before will not (necessarily) update.

See [[_]::fill_with]

Note: This replaces items, rather than changing their value, so components which were linked to the list before will not (necessarily) update.

See [[_]::first]

See [[_]::get]

See [[_]::get_unchecked]

See [[_]::iter]

See [[_]::last]

See [[_]::partition_point]

See [[_]::reverse]

See [[_]::rotate_left]

See [[_]::rotate_right]

See [[_]::sort]

See [[_]::sort_by]

See [[_]::sort_by]

See [[_]::sort_by]

See [[_]::sort]

See [[_]::sort_by]

See [[_]::sort_by]

See [[_]::starts_with]

See [[_]::swap]

Trait Implementations

Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.