State Management in Jetpack Compose

Sep 10 2024 · Kotlin 1.9.22, Android 34, Android Studio Iguana | 2023.2.1 Patch 2

Lesson 03: State Hoisting

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this demo, you’ll implement state hoisting to create reusable composable functions.

@Composable
fun FieldInputComponent(
  modifier: Modifier = Modifier,
  label: String,
  inputValue: String,
  onInputValueChanged: (String) -> Unit
) {
  Column(modifier = modifier) {
    OutlinedTextField(value = inputValue,
      onValueChange = onInputValueChanged,
      shape = RoundedCornerShape(16.dp),
      modifier = Modifier.fillMaxWidth(),
      label = {
        Text(text = "Enter Member's $label")
      })
    Spacer(modifier = Modifier.height(32.dp))
    Text(text = "$label is $inputValue")
  }
}
FieldInputComponent(
  label = "Name",
  inputValue = memberName,
  onInputValueChanged = { newValue ->
    memberName = newValue
  }
)
var memberEmail  by remember { mutableStateOf("") }
FieldInputComponent(
  label = "Email",
  inputValue = memberEmail,
  onInputValueChanged = { newValue ->
    memberEmail = newValue
  }
)
Spacer(modifier = Modifier.height(40.dp))
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 1 Next: Instruction 2