Can I pass down a `MutableState` into a `@Composab...
# compose
r
Can I pass down a
MutableState
into a
@Composable
function, and use
var value by mutableState
at the outer function and expect a recomposition once the inner function changes
MutableState
?
h
If I understand correctly what you want to achieve, yes you can. In fact some use cases may only be achieved using that approach
v
don't forget to
remember
🄁 1
s
h
I don’t get why you should pass the lambda, why not just receive the
SomeData
object directly?
s
Only if you need to defer the state read to a scope further down in your composable tree. It’s an optimization to reduce recompositions. 99% of the time you don’t need it. Sometimes however it is a good idea.
šŸ‘ 1
some use cases may only be achieved using that approach
No that isn’t true, you can always pass down the state read lamba + a lambda to change the state too. Which is always a better idea than passing MutableState at least.
r
Didn't need it after all, but thanks for the info, will keep in mind.
h
yeah I think I get it now, as long as the function is not re-created when whatever it is used inside to give the state changes, you will need to be careful with that, is not just ā€œreplacing X with Yā€ šŸ¤”. Otherwise it will be the same as sending the State directly