pub struct Value<T> { /* private fields */ }Expand description
A reactive value. Basic building block of app state.
Can be read or written.
use vertigo::{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) -> Self
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 Router implementation for example example.
Sourcepub fn set_force(&self, value: T)
pub fn set_force(&self, value: T)
Allows to set a new value if T doesn’t implement PartialEq.
This will always trigger a graph change even if the value stays the same.
Sourcepub fn get(&self, context: &Context) -> T
pub fn get(&self, context: &Context) -> T
Get the value.
Use this in callbacks. You can get Context object using transaction function.
During rendering you should directly embed the Value in dom! or use .render_value() method.
Returned T is cloned - it’s not reactive.
Sourcepub fn map<K: Clone + 'static, F: 'static + Fn(T) -> K>(
&self,
fun: F,
) -> Computed<K>
pub fn map<K: Clone + 'static, F: 'static + Fn(T) -> K>( &self, fun: F, ) -> Computed<K>
Reactively convert Value into Computed with provided transformation function applied.
pub fn id(&self) -> GraphId
pub fn deps(&self) -> &'static Dependencies
Sourcepub fn to_computed(&self) -> Computed<T>
pub fn to_computed(&self) -> Computed<T>
Reactively convert the `Value`` into Computed without any mapping.
Source§impl<T: Clone + PartialEq + 'static> Value<T>
impl<T: Clone + PartialEq + 'static> Value<T>
pub fn change(&self, change_fn: impl FnOnce(&mut T))
pub fn set(&self, 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>
};