Is it a supported pattern to pass around `MutableS...
# compose
n
Is it a supported pattern to pass around
MutableState
instances to implement "utility" composables? (Seems to work but does not follow the "state hoisting" pattern.)
Copy code
@Composable
fun <T : Any> ComboBox(
    state: MutableState<T?>,
    ...
) {
    ComboBox(
        selected = state.value,
        onChange = { state.value = it },
        ...
    )
}
Thanks.
m
You should avoid doing so because you'll end up with multiple sources of truth which can result in a hard to debug obscure bugs + it makes your api a bit more clunky.
๐Ÿ‘๐Ÿป 1
๐Ÿ™ 1
Consider accepting a lambda parameter instead.
๐Ÿ‘๐Ÿป 1
s
Make it take:
Copy code
param: T
setParam: (T) -> Unit
Or even
Copy code
getParam: () -> T
setParam: (T) -> Unit
There's never a good reason to make it take MutableState instead.
๐Ÿ‘๐Ÿป 1
๐Ÿ™ 1
๐Ÿ‘ 1
z
Lambdas are nice because at some point youโ€™ll need to actually do some on a value coming in or out and if you have a lambda you can just do it. Also makes it easier to write tests
๐Ÿ‘๐Ÿป 1
๐Ÿ™ 1