Skip to main content

run_in_mutable_snapshot

Function run_in_mutable_snapshot 

Source
pub fn run_in_mutable_snapshot<T>(
    block: impl FnOnce() -> T,
) -> Result<T, &'static str>
Expand description

Runs the provided closure inside a mutable snapshot and applies the result.

Use this function when you need to update MutableState from outside the composition or layout phase, typically in event handlers or async tasks.

§Why is this needed?

Cranpose uses a snapshot system (MVCC) to isolate state changes. Modifications made to MutableState are only visible to the current thread’s active snapshot. To make changes visible to the rest of the system (and trigger recomposition), they must be “applied” by committing the snapshot. This helper handles that lifecycle for you.

§Example

// Inside a button click handler
run_in_mutable_snapshot(|| {
    count.set(count.value() + 1);
});

§Important

ALL UI event handlers (keyboard, mouse, touch, animations) that modify state MUST use this function or dispatch_ui_event.