0

If X, Y are of type MutableState<String>, in the following snippet ComposableB, ComposableC and ComposableD are recomposed everytime X's state is changed.

Case 1

@Composable
fun ComposableA(
    val stateX: State<String>, // X
    val stateYProvider: () -> State<String>, // { Y }
) {
    ComposableB() {
        ComposableC(stateX.value)
        ComposableD(stateYProvider)
    }
}

But in the following snippet only ComposableB and ComposableC are recomposed everytime X's state is changed

Case 2

@Composable
fun ComposableA(
    val stateX: State<String>, // X
    val stateYProvider: () -> State<String>, // { Y }
) {
    ComposableB() {
        ComposableC(stateX.value)
        ComposableD({ stateYProvider() })
    }
}

Why is this so? Shouldn't Case 1 also recompose only ComposableB and ComposableC as ComposableD's arguments remain same when ComposableB's recomposition happens

Note: Passing states instead of values because every time the state changes ComposableA is getting recomposed, which is unnecessary

Madhu Kumar Dadi
  • 695
  • 8
  • 25
  • 1
    How exactly are you tracking recompositions? – Richard Onslow Roper May 16 '22 at 12:07
  • Logging at the start of each composable with static message – Madhu Kumar Dadi May 16 '22 at 12:31
  • You don't need state in lambda to change for recomposition to happen. If function parameter is a new value in your example new instance of lambda it should be recomposed and which functions should be recomposed is based on closest scope that a value is read in, function without inline are not considered as a scope. So anything whether reading a value of parameter or not is susceptible to recomposition in which call site they are. You should check hash code for lambdas to see if they are the same instances. And for scopes and recomposition you might check these questions. – Thracian May 16 '22 at 12:53
  • [Smart recomposition](https://stackoverflow.com/questions/70257459/jetpack-compose-smart-recomposition), and [this one](https://stackoverflow.com/questions/71045838/why-does-mutablestateof-without-remember-work-sometimes?noredirect=1&lq=1) – Thracian May 16 '22 at 12:54
  • 2
    And instead of passing values why do you pass a lambda with mutableState. It looks very complicated – Thracian May 16 '22 at 12:55
  • @Thracian thanks for the suggestions. I am passing states because this value is passing through too many composables so whenever this state is changed all the composables are getting recomposed – Madhu Kumar Dadi May 16 '22 at 13:08
  • You can check [this](https://developer.android.com/jetpack/compose/performance#defer-reads) – Madhu Kumar Dadi May 16 '22 at 13:15
  • @MadhuKumarDadi do you have a complete snippet of this codes? not the actual code these represents, what I mean is the `ComposableB` the `ComposableC` and how do you use/call `ComposableA`, I'm trying to re-create the issue you said, but I only see `C` and `D` getting re-composed, not `B` for case 1 – z.g.y Dec 11 '22 at 02:13

0 Answers0