remember_with_key

Function remember_with_key 

Source
pub fn remember_with_key<K, F, T>(key: K, init: F) -> State<T>
where K: Hash, F: FnOnce() -> T, T: Send + Sync + 'static,
Expand description

Remember a value across frames with an explicit key.

This function allows a component to “remember” state across recomposition (build) passes, using a user-provided key to identify the state. This is particularly useful for state generated inside loops or dynamic collections where the execution order might change.

The init closure is executed only once — when the key is first encountered. On subsequent updates with the same key, the stored value is returned and init is not called.

§Interior mutability

This function returns a State<T> handle that internally uses an Arc<RwLock<T>>. Use with, with_mut, get, or set to read or update the value without handling synchronization primitives directly.

§Comparison with remember

Use this function when the state is generated inside a loop or dynamic collection where the execution order might change. In other cases, remember is sufficient.

§Panics

This function must be called during a component’s build/render phase. Calling it during the measure or input handling phases will panic.