Struct leptos::StoredValue
source · pub struct StoredValue<T>where
T: 'static,{ /* private fields */ }
Expand description
A non-reactive wrapper for any value, which can be created with store_value
.
If you want a reactive wrapper, use create_signal
.
This allows you to create a stable reference for any value by storing it within
the reactive system. Like the signal types (e.g., ReadSignal
and RwSignal
), it is Copy
and 'static
. Unlike the signal
types, it is not reactive; accessing it does not cause effects to subscribe, and
updating it does not notify anything else.
Implementations§
source§impl<T> StoredValue<T>
impl<T> StoredValue<T>
sourcepub fn get_value(&self) -> Twhere
T: Clone,
pub fn get_value(&self) -> Twhere
T: Clone,
Returns a clone of the current stored value.
§Panics
Panics if you try to access a value owned by a reactive node that has been disposed.
§Examples
#[derive(Clone)]
pub struct MyCloneableData {
pub value: String,
}
let data = store_value(MyCloneableData { value: "a".into() });
// calling .get_value() clones and returns the value
assert_eq!(data.get_value().value, "a");
// can be `data().value` on nightly
// assert_eq!(data().value, "a");
sourcepub fn try_get_value(&self) -> Option<T>where
T: Clone,
pub fn try_get_value(&self) -> Option<T>where
T: Clone,
Same as StoredValue::get_value
but will not panic by default.
sourcepub fn with_value<U>(&self, f: impl FnOnce(&T) -> U) -> U
pub fn with_value<U>(&self, f: impl FnOnce(&T) -> U) -> U
Applies a function to the current stored value and returns the result.
§Panics
Panics if you try to access a value owned by a reactive node that has been disposed.
§Examples
pub struct MyUncloneableData {
pub value: String,
}
let data = store_value(MyUncloneableData { value: "a".into() });
// calling .with_value() to extract the value
assert_eq!(data.with_value(|data| data.value.clone()), "a");
sourcepub fn try_with_value<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>
pub fn try_with_value<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>
Same as StoredValue::with_value
but returns [Some(O)]
only if
the stored value has not yet been disposed. None
otherwise.
sourcepub fn update_value(&self, f: impl FnOnce(&mut T))
pub fn update_value(&self, f: impl FnOnce(&mut T))
Updates the stored value.
§Examples
pub struct MyUncloneableData {
pub value: String,
}
let data = store_value(MyUncloneableData { value: "a".into() });
data.update_value(|data| data.value = "b".into());
assert_eq!(data.with_value(|data| data.value.clone()), "b");
use leptos_reactive::*;
pub struct MyUncloneableData {
pub value: String,
}
let data = store_value(MyUncloneableData { value: "a".into() });
let updated = data.try_update_value(|data| {
data.value = "b".into();
data.value.clone()
});
assert_eq!(data.with_value(|data| data.value.clone()), "b");
assert_eq!(updated, Some(String::from("b")));
§Panics
Panics if there is no current reactive runtime, or if the stored value has been disposed.
sourcepub fn try_update_value<O>(self, f: impl FnOnce(&mut T) -> O) -> Option<O>
pub fn try_update_value<O>(self, f: impl FnOnce(&mut T) -> O) -> Option<O>
Same as Self::update_value
, but returns [Some(O)
] if the
stored value has not yet been disposed, None
otherwise.
sourcepub fn set_value(&self, value: T)
pub fn set_value(&self, value: T)
Sets the stored value.
§Examples
pub struct MyUncloneableData {
pub value: String,
}
let data = store_value(MyUncloneableData { value: "a".into() });
data.set_value(MyUncloneableData { value: "b".into() });
assert_eq!(data.with_value(|data| data.value.clone()), "b");
sourcepub fn try_set_value(&self, value: T) -> Option<T>
pub fn try_set_value(&self, value: T) -> Option<T>
Same as Self::set_value
, but returns None
if the
stored value has not yet been disposed, [Some(T)
] otherwise.
source§impl<T> StoredValue<T>
impl<T> StoredValue<T>
sourcepub fn new(value: T) -> StoredValue<T>
pub fn new(value: T) -> StoredValue<T>
Creates a non-reactive wrapper for any value by storing it within the reactive system.
Like the signal types (e.g., ReadSignal
and RwSignal
), it is Copy
and 'static
. Unlike the signal
types, it is not reactive; accessing it does not cause effects to subscribe, and
updating it does not notify anything else.
// this structure is neither `Copy` nor `Clone`
pub struct MyUncloneableData {
pub value: String
}
// ❌ this won't compile, as it can't be cloned or copied into the closures
let data = MyUncloneableData { value: "a".into() };
let callback_a = move || data.value == "a";
let callback_b = move || data.value == "b";
// this structure is neither `Copy` nor `Clone`
pub struct MyUncloneableData {
pub value: String,
}
// ✅ you can move the `StoredValue` and access it with .with_value()
let data = StoredValue::new(MyUncloneableData { value: "a".into() });
let callback_a = move || data.with_value(|data| data.value == "a");
let callback_b = move || data.with_value(|data| data.value == "b");
§Panics
Panics if there is no current reactive runtime.
Trait Implementations§
source§impl<T> Clone for StoredValue<T>
impl<T> Clone for StoredValue<T>
source§fn clone(&self) -> StoredValue<T>
fn clone(&self) -> StoredValue<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T> Debug for StoredValue<T>
impl<T> Debug for StoredValue<T>
source§impl<T> Default for StoredValue<T>where
T: Default,
impl<T> Default for StoredValue<T>where
T: Default,
source§fn default() -> StoredValue<T>
fn default() -> StoredValue<T>
source§impl<T> Hash for StoredValue<T>
impl<T> Hash for StoredValue<T>
source§impl<T> PartialEq for StoredValue<T>
impl<T> PartialEq for StoredValue<T>
source§fn eq(&self, other: &StoredValue<T>) -> bool
fn eq(&self, other: &StoredValue<T>) -> bool
self
and other
values to be equal, and is used
by ==
.impl<T> Copy for StoredValue<T>
impl<T> Eq for StoredValue<T>
Auto Trait Implementations§
impl<T> Freeze for StoredValue<T>
impl<T> RefUnwindSafe for StoredValue<T>where
T: RefUnwindSafe,
impl<T> Send for StoredValue<T>where
T: Send,
impl<T> Sync for StoredValue<T>where
T: Sync,
impl<T> Unpin for StoredValue<T>where
T: Unpin,
impl<T> UnwindSafe for StoredValue<T>where
T: UnwindSafe,
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
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more