To build a dropdown, I have the following input to...
# compose
z
To build a dropdown, I have the following input to a composable:
Copy code
selectedId: String,
// the `selectedId` will always be in this list
options: List<IdWithDisplayName>
How do I properly use the
selectedId
to lookup the
displayName
in the list of options?
m
There's no state object to derive from here. Just remove all the state handling since the state has been hoisted out of
MyDropdown
. If that lookup is proving too slow, then doing a remember with the
selectedId
and
options
as keys could cache the search. Also may want to consider an
ImmutableList
so that Compose can optimize.
s
Are you going to be changing the selectedId inside this composable? Probably not, and if yes, it’d probably be with a lambda to do this further up. Also exactly as mkrussel says, you don’t have any MutableState to use derivedStateOf, it’s all just parameters coming in the function, so it wouldn’t help you at all. Simply do a:
Copy code
val selectedDisplayName = remember(selectedId, options) {
  options.first { it.id = selectedId }.displayName
}
And this should be good enough.