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

reactormonk

09/20/2023, 3:12 PM
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

Hrodrick

09/20/2023, 3:22 PM
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

vide

09/20/2023, 3:45 PM
don't forget to
remember
🥁 1
s

Stylianos Gakis

09/20/2023, 4:30 PM
h

Hrodrick

09/20/2023, 4:37 PM
I don’t get why you should pass the lambda, why not just receive the
SomeData
object directly?
s

Stylianos Gakis

09/20/2023, 4:38 PM
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

reactormonk

09/20/2023, 4:40 PM
Didn't need it after all, but thanks for the info, will keep in mind.
h

Hrodrick

09/20/2023, 4:41 PM
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