https://kotlinlang.org logo
#compose
Title
# compose
d

Dhya Pacifica

09/20/2023, 3:11 AM
Trying to hoist:
Copy code
val radioOptions = listOf("Calls", "Missed", "Friends")
val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[0]) }
Which is from the Compose RadioButton example. Am familiar with hoisting when there is one parameter, but in this case there is the extra parameter onOptionSelected which is throwing me off. Currently I have the child composable accepting the two parameters like:
Copy code
@Composable
fun QuestionRadioButtons(
    selectedOption: String,
    onOptionSelected: (String) -> (Unit)
) { ... }
k

Kirill Grouchnikov

09/20/2023, 3:40 AM
What’s the question?
d

Dhya Pacifica

09/20/2023, 3:42 AM
What is onOptionSelected and how is it saved by mutableStateOf ?
I'm not sure why onOptionSelected is needed when selectedOption already contains the state string.
k

Kirill Grouchnikov

09/20/2023, 3:44 AM
Your UI composable gets
x
as the current state to “display” in the UI, and an
onXSomething
as the lambda to call upwards when the user interacts with that data display, and that interaction should lead to a state change
State update happens outside of your composable, and that place may or may not “accept” the new proposed state
d

Dhya Pacifica

09/20/2023, 3:47 AM
Where or how is the onOptionSelected lamba defined?
Thank you!
k

Kirill Grouchnikov

09/20/2023, 3:48 AM
In this particular simplified case, the
onOptionSelected
is a lambda generated for you with no validation, custom acceptance or rejection, persistence or any other part of it you would have in the real app.
👍 1
3 Views