pub struct SnapshotState<T>where
T: Clone + 'static,{ /* private fields */ }Expand description
Cheap copyable mutable view of a state cell.
Ownership lives elsewhere: a composition slot, an OwnedMutableState, or
the runtime for states created with mutableStateOf / MutableState::with_runtime.
Implementations§
Source§impl<T> MutableState<T>where
T: Clone + 'static,
impl<T> MutableState<T>where
T: Clone + 'static,
Sourcepub fn with_runtime(value: T, runtime: RuntimeHandle) -> MutableState<T>
pub fn with_runtime(value: T, runtime: RuntimeHandle) -> MutableState<T>
Creates a runtime-owned persistent state handle.
Sourcepub fn is_alive(&self) -> bool
pub fn is_alive(&self) -> bool
Returns true if the underlying state cell is still alive (not released).
Sourcepub fn try_with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R>
pub fn try_with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R>
Like with, but returns None if the state cell has been released.
Use this in frame callbacks, fling animations, and other contexts where the owning composition group may have been disposed (e.g., tab switch).
Sourcepub fn try_value(&self) -> Option<T>
pub fn try_value(&self) -> Option<T>
Like value, but returns None if the state cell has been released.
pub fn as_state(&self) -> State<T>
Sourcepub fn try_retain(&self) -> Option<OwnedMutableState<T>>
pub fn try_retain(&self) -> Option<OwnedMutableState<T>>
Upgrades this handle into an owning state value if the cell is still alive.
Sourcepub fn retain(&self) -> OwnedMutableState<T>
pub fn retain(&self) -> OwnedMutableState<T>
Upgrades this handle into an owning state value.
pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R
pub fn update<R>(&self, f: impl FnOnce(&mut T) -> R) -> R
Sourcepub fn replace(&self, value: T)
pub fn replace(&self, value: T)
Sets a new value, silently skipping the write if the state cell was released.
State cells can be released between when a SideEffect closure captures a
MutableState handle and when the side effect actually runs (command
application disposes composition groups, freeing their state slots, before
side effects execute). Using with_typed_opt instead of with_typed
avoids a panic on the stale handle.
pub fn set_value(&self, value: T)
pub fn set(&self, value: T)
pub fn value(&self) -> T
pub fn get(&self) -> T
Sourcepub fn get_non_reactive(&self) -> T
pub fn get_non_reactive(&self) -> T
Gets the current value WITHOUT subscribing to recomposition.
Use this in layout/measure/draw phases to read state without causing the current composition scope to recompose when the state changes.
§When to use
- In modifier nodes (like ScrollNode) during measure()
- In any layout phase code that reads state but shouldn’t trigger recomposition
§When NOT to use
- In composable functions that should update when state changes
- When you want reactive UI updates
Trait Implementations§
Source§impl<T> Clone for MutableState<T>where
T: Clone + 'static,
impl<T> Clone for MutableState<T>where
T: Clone + 'static,
Source§fn clone(&self) -> MutableState<T>
fn clone(&self) -> MutableState<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more