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

deviant

08/03/2022, 8:25 AM
hey guys since
derivedStateOf
should be always wrapped into
remember
, i decided to make this helper
Copy code
@Composable
fun <T> rememberDerived(calculation: () -> T) = remember {
    derivedStateOf(calculation)
}
do you see any issues with it?
🤔 2
ok. it doesn't work as i was expecting! the state isn't deriving. not sure why, probably some internal compose magic
also i saw somewhere in chats that derivedStateOf should be always wrapped
s

Stylianos Gakis

08/03/2022, 1:07 PM
Yes you do need to
remember
your
derivedStateOf
calls. I don’t know about the function above though, if compose can still do proper tracking of what is read inside the
derivedStateOf
to only recompose when it needs to. For one maybe this function is introducing a new recomposition scope? How does it work if you make it inline, does it do what you expect? Just a wild guess though. I’d personally go for simple
remember { derivedStateOf {} }
since sometimes you do want to pass some things as keys to the
remember
function if they’re not state objects. Since
derivedStateOf
only knows how to read and automatically keep up to date with state objects, not anything you put in there.
f

FunkyMuse

08/03/2022, 4:32 PM
https://kotlinlang.slack.com/archives/CJLTWPH7S/p1659521757388299?thread_ts=1659515103.204379&amp;cid=CJLTWPH7S This is changing because the highPriorityKeyWords changes and the derived is recalculated
s

Stylianos Gakis

08/03/2022, 5:05 PM
Regarding the docs sample, do you have a link to it? I'd guess the todoTasks is a state object while highPriorityKeywords is not a state object, hence you'd need to do that. But I'd love to see the docs link
s

shikasd

08/03/2022, 5:17 PM
A) you need to remember derived state object, otherwise it will create a new state each composition B) if you can confirm that your helper doesn't work, please file a bug, this usage looks legit to me
s

Sean McQuillan [G]

08/03/2022, 10:55 PM
very minor drive by comment - you can make @Composable funs inline, which has the effect of making them inline, and never-skippable
this is already never-skippable due to return value
d

deviant

08/04/2022, 9:00 AM
the proposed helper function seems works fine. i added optional
key
param to pass into
remember
. the bug i observe is coming from somewhere else. and i can't reproduce it on hello world project (my widget isn't being updated despite the recomposition is being triggered). will file a bug when have an evidences
5 Views