0
0
Android Kotlinmobile~10 mins

State hoisting pattern in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a mutable state variable in a composable function.

Android Kotlin
var text by remember { mutableStateOf("") }
TextField(value = [1], onValueChange = { text = it })
Drag options to blanks, or click blank then click option'
Avalue
Bit
Cstate
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'it' instead of the state variable for the value parameter.
Using a variable that is not declared or initialized.
2fill in blank
medium

Complete the code to hoist the state variable out of the child composable.

Android Kotlin
@Composable
fun Parent() {
  var text by remember { mutableStateOf("") }
  Child([1], onTextChange = { text = it })
}

@Composable
fun Child(text: String, onTextChange: (String) -> Unit) {
  TextField(value = text, onValueChange = onTextChange)
}
Drag options to blanks, or click blank then click option'
Atext
Bit
Cvalue
Dstate
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'it' instead of the state variable.
Not passing the state variable at all.
3fill in blank
hard

Fix the error in the state hoisting pattern by completing the code.

Android Kotlin
@Composable
fun Parent() {
  var text by remember { mutableStateOf("") }
  Child(text = [1], onTextChange = { text = it })
}

@Composable
fun Child(text: String, onTextChange: (String) -> Unit) {
  TextField(value = text, onValueChange = onTextChange)
}
Drag options to blanks, or click blank then click option'
Avalue
Bit
Ctext
Dstate
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'it' or other variables instead of the state variable.
Not passing any value to the child.
4fill in blank
hard

Fill both blanks to complete the state hoisting pattern with a callback.

Android Kotlin
@Composable
fun Parent() {
  var text by remember { mutableStateOf("") }
  Child([1], onTextChange = [2])
}

@Composable
fun Child(text: String, onTextChange: (String) -> Unit) {
  TextField(value = text, onValueChange = onTextChange)
}
Drag options to blanks, or click blank then click option'
Atext
B{ newText -> text = newText }
Cit
D{ text = it }
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong variable or lambda syntax.
Using 'it' directly without a lambda.
5fill in blank
hard

Fill all three blanks to complete the state hoisting pattern with a stateless child composable.

Android Kotlin
@Composable
fun Parent() {
  var text by remember { mutableStateOf("") }
  Child([1], onTextChange = [2])
}

@Composable
fun Child([3]: String, onTextChange: (String) -> Unit) {
  TextField(value = [3], onValueChange = onTextChange)
}
Drag options to blanks, or click blank then click option'
Atext
B{ newText -> text = newText }
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent parameter names between parent and child.
Not passing the lambda correctly.