pub struct Value<T> { /* private fields */ }
Expand description
A reactive value. Basic building block of app state.
Can be read or written.
use vertigo::{Computed, Value, transaction};
let value = Value::new(5);
transaction(|context| {
assert_eq!(value.get(context), 5);
});
value.set(10);
transaction(|context| {
assert_eq!(value.get(context), 10);
});
Implementations§
source§impl<T: Clone + 'static> Value<T>
impl<T: Clone + 'static> Value<T>
pub fn new(value: T) -> Value<T>
sourcepub fn with_connect<F>(value: T, create: F) -> Computed<T>
pub fn with_connect<F>(value: T, create: F) -> Computed<T>
Create a value that is connected to a generator, where value
parameter is a starting value,
and create
function takes care of updating it.
See game of life example.
pub fn set(&self, value: T)
pub fn get(&self, context: &Context) -> T
pub fn change(&self, change_fn: impl FnOnce(&mut T))
pub fn map<K: Clone + 'static, F: 'static + Fn(T) -> K>( &self, fun: F ) -> Computed<K>
pub fn id(&self) -> GraphId
pub fn deps(&self) -> &'static Dependencies
pub fn to_computed(&self) -> Computed<T>
source§impl<T: Clone + PartialEq + 'static> Value<T>
impl<T: Clone + PartialEq + 'static> Value<T>
pub fn set_value_and_compare(&self, value: T)
source§impl<T: Clone + PartialEq + 'static> Value<T>
impl<T: Clone + PartialEq + 'static> Value<T>
sourcepub fn render_value(&self, render: impl Fn(T) -> DomNode + 'static) -> DomNode
pub fn render_value(&self, render: impl Fn(T) -> DomNode + 'static) -> DomNode
Render value (reactively transforms T
into DomNode
)
See computed_tuple if you want to render multiple values in a handy way.
use vertigo::{dom, Value};
let my_value = Value::new(5);
let element = my_value.render_value(|bare_value| dom! { <div>{bare_value}</div> });
dom! {
<div>
{element}
</div>
};
sourcepub fn render_value_option(
&self,
render: impl Fn(T) -> Option<DomNode> + 'static
) -> DomNode
pub fn render_value_option( &self, render: impl Fn(T) -> Option<DomNode> + 'static ) -> DomNode
Render optional value (reactively transforms Option<T>
into Option<DomNode>
)
See computed_tuple if you want to render multiple values in a handy way.
use vertigo::{dom, Value};
let value1 = Value::new(Some(5));
let value2 = Value::new(None::<i32>);
let element1 = value1.render_value_option(|bare_value|
bare_value.map(|value| dom! { <div>{value}</div> })
);
let element2 = value2.render_value_option(|bare_value|
match bare_value {
Some(value) => Some(dom! { <div>{value}</div> }),
None => Some(dom! { <div>"default"</div> }),
}
);
dom! {
<div>
{element1}
{element2}
</div>
};
source§impl<T: PartialEq + Clone + 'static, L: IntoIterator<Item = T> + Clone + PartialEq + 'static> Value<L>
impl<T: PartialEq + Clone + 'static, L: IntoIterator<Item = T> + Clone + PartialEq + 'static> Value<L>
sourcepub fn render_list<K: Eq + Hash>(
&self,
get_key: impl Fn(&T) -> K + 'static,
render: impl Fn(&T) -> DomNode + 'static
) -> DomNode
pub fn render_list<K: Eq + Hash>( &self, get_key: impl Fn(&T) -> K + 'static, render: impl Fn(&T) -> DomNode + 'static ) -> DomNode
Render iterable value (reactively transforms Iterator<T>
into Node with list of rendered elements )
use vertigo::{dom, Value};
let my_list = Value::new(vec![
(1, "one"),
(2, "two"),
(3, "three"),
]);
let elements = my_list.render_list(
|el| el.0,
|el| dom! { <div>{el.1}</div> }
);
dom! {
<div>
{elements}
</div>
};
Trait Implementations§
source§impl<T: PartialEq> PartialEq for Value<T>
impl<T: PartialEq> PartialEq for Value<T>
source§impl<T: Clone + 'static> ToComputed<T> for &Value<T>
impl<T: Clone + 'static> ToComputed<T> for &Value<T>
fn to_computed(&self) -> Computed<T>
source§impl<T: Clone + 'static> ToComputed<T> for Value<T>
impl<T: Clone + 'static> ToComputed<T> for Value<T>
fn to_computed(&self) -> Computed<T>
Auto Trait Implementations§
impl<T> Freeze for Value<T>
impl<T> !RefUnwindSafe for Value<T>
impl<T> !Send for Value<T>
impl<T> !Sync for Value<T>
impl<T> Unpin for Value<T>
impl<T> !UnwindSafe for Value<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more