vector / com.haroldadmin.vector / withState
withState¶
inline fun <S :
VectorState
> withState(viewModel:
VectorViewModel
<
S
>, crossinline block: (
S
) ->
Unit
):
Unit
A convenience function to access current state and execute an action on it.
Parameters¶
S
- The type of the current state
block
-
The action to be performed using the current state
Example:
class MyViewModel(): VectorViewModel()
class MyFragment(): VectorFragment { onViewCreated(...) { withState(viewModel) { state -> if (state.isPremiumUser) { premiumFeature.enable() } } } }
Note: The state provided to the block is not guaranteed to be the latest state, because there might be other state mutation blocks in the State Store's queue
Warning: This WILL cause your app to crash if you create your ViewModels without initial state and fail to provide it later, before calling this function.