pub trait Binding<S>: Clone + Copy + 'static {
// Required methods
fn get<'a>(&self, cx: &'a Context) -> &'a S;
fn get_mut<'a>(&self, cx: &'a mut Context) -> &'a mut S;
// Provided methods
fn with<T>(&self, cx: &Context, f: impl FnOnce(&S) -> T) -> T { ... }
fn with_mut<T>(&self, cx: &mut Context, f: impl FnOnce(&mut S) -> T) -> T { ... }
}
Expand description
Reads or writes a value owned by a source-of-truth.
Required Methods§
fn get<'a>(&self, cx: &'a Context) -> &'a S
fn get_mut<'a>(&self, cx: &'a mut Context) -> &'a mut S
Provided Methods§
sourcefn with_mut<T>(&self, cx: &mut Context, f: impl FnOnce(&mut S) -> T) -> T
fn with_mut<T>(&self, cx: &mut Context, f: impl FnOnce(&mut S) -> T) -> T
Examples found in repository?
examples/todo_list.rs (line 9)
3 4 5 6 7 8 9 10 11 12 13 14 15
fn add_button(todos: impl Binding<Vec<String>>) -> impl View {
state(String::new, move |name, _| {
hstack((
text_editor(name),
button(text("Add Item"), move |cx| {
let name_str = cx[name].clone();
todos.with_mut(cx, |todos| todos.push(name_str));
// Gotta fix a bug in text_editor!
// cx[name] = String::new();
}),
))
})
}