1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! # State
//!
//! [`rememberMutableStateOf`] retains observable state across recomposition.
//! A composable subscribes to state through a read. A later write invalidates
//! the subscribed scope. This counter updates after a button click:
//!
//! ```no_run
//! use cranpose::*;
//!
//! #[composable]
//! fn Counter() {
//! let count = rememberMutableStateOf(|| 0i32);
//!
//! Button(
//! Modifier::empty().padding(10.0),
//! ButtonSpec::default(),
//! move || count.set(count.get() + 1),
//! move || {
//! Text(
//! format!("Count: {}", count.get()),
//! Modifier::empty(),
//! TextStyle::default(),
//! );
//! },
//! );
//! }
//!
//! fn main() {}
//! ```
//!
//! State handles are `Copy`. Move a handle into each callback to share the value.
//!
//! | Call | Purpose |
//! | --- | --- |
//! | [`remember`] | A value retained for the composition lifetime. |
//! | [`rememberKeyed`] | A value recomputed only when its key changes. |
//! | [`rememberMutableStateOf`] | Observable state. |
//! | [`rememberUpdatedState`] | A current callback or value for a long-lived effect. |
//! | [`rememberCoroutineScope`] | A scope for work started from an event handler. |
//! | [`mutableStateOf`] | State owned outside the composition. |
//!
//! A state read inside `draw_behind` or the lazy `graphics_layer` closure
//! subscribes the node's visual phase. Updates then invalidate the draw phase.
//! A layout read subscribes the layout phase; a composition read subscribes the
//! composition scope. Place each read in the phase which needs the value.
//!
//! [`remember`]: crate::prelude::remember
//! [`rememberKeyed`]: crate::prelude::rememberKeyed
//! [`rememberMutableStateOf`]: crate::prelude::rememberMutableStateOf
//! [`rememberUpdatedState`]: crate::prelude::rememberUpdatedState
//! [`rememberCoroutineScope`]: crate::prelude::rememberCoroutineScope
//! [`mutableStateOf`]: crate::prelude::mutableStateOf