derivedStateOf
public fun <T> derivedStateOf(calculation: () -> T): State<T>
Creates a State object whose State.value is the result of calculation. The result of calculation will be cached in such a way that calling State.value repeatedly will not cause calculation to be executed multiple times, but reading State.value will cause all State objects that got read during the calculation to be read in the current Snapshot, meaning that this will correctly subscribe to the derived state objects if the value is being read in an observed context such as a Composable function. Derived states without mutation policy trigger updates on each dependency change. To avoid invalidation on update, provide suitable SnapshotMutationPolicy through derivedStateOf overload.
For trivial calculation implementations, this caching may be more expensive than computing the value each time due to the snapshot management overhead. In these situations, prefer computedStateOf, which omits the caching behavior but yields a state that takes on the value of a calculation that will only invalidate readers when the calculation returns a new value.
Parameters
| calculation | the calculation to create the value this state object represents. |
derivedStateOf
public fun <T> derivedStateOf(policy: SnapshotMutationPolicy<T>, calculation: () -> T): State<T>
Creates a State object whose State.value is the result of calculation. The result of calculation will be cached in such a way that calling State.value repeatedly will not cause calculation to be executed multiple times, but reading State.value will cause all State objects that got read during the calculation to be read in the current Snapshot, meaning that this will correctly subscribe to the derived state objects if the value is being read in an observed context such as a Composable function.
For trivial calculation implementations, this caching may be more expensive than computing the value each time due to the snapshot management overhead. In these situations, prefer computedStateOf, which omits the caching behavior but yields a state that takes on the value of a calculation that will only invalidate readers when the calculation returns a new value.
Parameters
| policy | mutation policy to control when changes to the calculation result trigger update. |
| calculation | the calculation to create the value this state object represents. |