Android & Jetpack Compose (50 Qs)•Part 1: Architecture, Jetpack Compose & Core Components (1 - 10)
What is State Hoisting in Jetpack Compose?
senior LevelMost AskedCode ExampleComponentsClosures & ScopeDocker & Containers
In Simple Words: State hoisting is a programming pattern of moving state up to a caller component to make a composable stateless:
- Replaces internal state with two parameters:
value: T: The current value to display.onValueChange: (T) -> Unit: An event callback lambda requesting the value to change.
- Benefits: Makes composables reusable, decoupled, and easy to unit test.
kotlin
// Stateless hoisted composable:
@Composable
fun NameInput(name: String, onNameChange: (String) -> Unit) {
TextField(value = name, onValueChange = onValueChange, label = { Text("Name") })
}