Thread
#compose
    Archie

    Archie

    1 year ago
    Hi everyone, Just would like to clarify something about
    derivedStateOf
    . In the official Documentation here: https://developer.android.com/jetpack/compose/side-effects There is this sample code:
    @Composable
    fun TodoList(
        highPriorityKeywords: List<String> = listOf("Review", "Unblock", "Compose")
    ) {
        val todoTasks = remember { mutableStateListOf<String>() }
    
        // Calculate high priority tasks only when the todoTasks or
        // highPriorityKeywords change, not on every recomposition
        val highPriorityTasks by remember(todoTasks, highPriorityKeywords) {
            derivedStateOf {
                todoTasks.filter { it.containsWord(highPriorityKeywords) }
            }
        }
    
        Box(Modifier.fillMaxSize()) {
            LazyColumn {
                items(highPriorityTasks) { /* ... */ }
                items(todoTasks) { /* ... */ }
            }
            /* Rest of the UI where users can add elements to the list */
        }
    }
    Wouldn’t the same thing would be achieve even without the
    derivedStateOf
    ? Like:
    @Composable
    fun TodoList(
        highPriorityKeywords: List<String> = listOf("Review", "Unblock", "Compose")
    ) {
        val todoTasks = remember { mutableStateListOf<String>() }
    
        // Calculate high priority tasks only when the todoTasks or
        // highPriorityKeywords change, not on every recomposition
        val highPriorityTasks by remember(todoTasks.value, highPriorityKeywords) {
            todoTasks.filter { it.containsWord(highPriorityKeywords) }
        }
    
        Box(Modifier.fillMaxSize()) {
            LazyColumn {
                items(highPriorityTasks) { /* ... */ }
                items(todoTasks) { /* ... */ }
            }
            /* Rest of the UI where users can add elements to the list */
        }
    }
    Wouldn’t this do the exact same thing?
    Dominaezzz

    Dominaezzz

    1 year ago
    derivedStateOf
    can potentially reduce the number of recompositions since it's value is calculated and subscribed to lazily.
    Archie

    Archie

    1 year ago
    Hi @Dominaezzz, Thanks for the reply ❤️. So essentially, the first block of code only recomposes the
    LazyColumn
    but the second one, recomposes the whole
    TodoList(..)
    composable right? Thats the main difference?
    Dominaezzz

    Dominaezzz

    1 year ago
    That's about right yh.
    Archie

    Archie

    1 year ago
    I’m still confuse though, if
    val todoTasks = remember { mutableStateListOf<String>() }
    updates its value, wouldn’t that recompose the whole
    TodoList(…)
    anyway?
    Oh now I get it. The main difference is that
    derivedStateOf
    converts it into a
    State<T>
    .
    Dominaezzz

    Dominaezzz

    1 year ago
    Exactly!
    l

    lhwdev

    1 year ago
    And derivedStateOf can be used outside composable functions. But I wonder why remember key is needed to remember
    derivedStateOf
    , isn't it sufficient to write
    val highPriorityTasks by remember { derivedStateOf { ... } }
    as
    derivedStateOf
    tracks its read by itself?
    mcpiroman

    mcpiroman

    1 year ago
    And is the remembereven needed? Wouldn't derivedStateOfcache it's output if the inputs don't change just like rememberwould?
    l

    lhwdev

    1 year ago
    Remember is needed, or when recomposing DerivedState is unnecessarily recreated and values can be recalculated. Note that derivedStateOf is similar to
    mutableStateOf
    , can be used outside Composable.