Function avalanche::hooks::vec [−][src]
pub fn vec<'a, T: 'static, F: FnOnce() -> Vec<T>>(
ctx: Context<'a>,
f: F
) -> (Tracked<&'a Vec<T>>, VecSetter<T>)
Expand description
Like state, but returns a reference to a tracked::Vec.
Takes in a function F
that returns
a default Vec. The return value has fine-grained tracking: instead of only the
whole vec being updated or not updated, each individual element is also tracked. For more information on how that works see
tracked::Vec.
Note that vec
returns a Tracked<tracked::Vec>
, which is marked as updated if
any of its children are updated. However, an individual element’s update status overrides this where appropriate.
Example
use avalanche::{component, tracked, View, vec};
use avalanche_web::components::{Div, H2, Button, Text};
#[component]
fn DynamicChildren() -> View {
let (data, update_data) = vec(self, || vec!["child 1"]);
let children = tracked!(data)
.iter()
.enumerate()
.map(|(n, text)| Text!(key: n.to_string(), tracked!(text))).collect::<Vec<_>>();
Div!([
Button!(
on_click: move |_| update_data.update(|data| data.push("another child")),
child: Text!("+")
),
Div!(tracked!(children))
])
}