Complete the code to declare a mutable state variable in a composable function.
var text by remember { mutableStateOf("") }
TextField(value = [1], onValueChange = { text = it })The value parameter of TextField should be the state variable text to reflect the current input.
Complete the code to hoist the state variable out of the child composable.
@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)
}The text state is passed down from the parent to the child composable to hoist the state.
Fix the error in the state hoisting pattern by completing the code.
@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)
}The text variable must be passed as the text argument to the child composable.
Fill both blanks to complete the state hoisting pattern with a callback.
@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)
}The parent passes the current text state and a lambda to update it when the child calls onTextChange.
Fill all three blanks to complete the state hoisting pattern with a stateless child composable.
@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)
}The parent passes text and a lambda to update it. The child receives the state as input and uses it in TextField.