Are nested composable declarations considered as an antipattern?

35 Views Asked by At

What are the differences in these two apart from not having to pass all the variables in nested calls? Are there any performance differences?

/// Approach 1: Nested declarations
@Composable
fun MyView1(currentNum: Int) {
    @Composable
    fun InnerView(num: Int) {
         Text(
              "$num",
              color = if (currentNum == num) Color.Red else Color.Black
         )
    }
    for (i in 0..currentNum) {
        InnerView(i)
    }
}

/// Approach 2: Normal top level declarations
@Composable
fun MyView2(currentNum: Int) {
    for (i in 0..currentNum) {
        InnerView(i, currentNum)
    }
}
@Composable
fun InnerView(num: Int, currentNum: Int) {
    Text(
        "$num",
        color = if (currentNum == num) Color.Red else Color.Black
    )
}
0

There are 0 best solutions below