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

Colton Idle

06/30/2021, 10:22 PM
Feel like I'm missing something basic. Anyone know why this doesn't compile?
Copy code
var myCount by remember { mutableStateOf(0) }
val shouldDisplay = remember { derivedStateOf { (myCount > 1) } }
m

Mitchell Skaggs

06/30/2021, 10:25 PM
Make sure you have all the imports. There are a lot of extension functions required for the functionality. Intellij can help add imports automatically.
☝🏼 1
t

Tash

06/30/2021, 10:26 PM
Seems to work… what error are you getting?
Yea, make sure to have these imports:
Copy code
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
c

Colton Idle

06/30/2021, 10:27 PM
All of my following composable fail after I add that line. Let me check the imports.
t

Tash

06/30/2021, 10:28 PM
Seems to work ok…yea might be something to do with your env/file?
c

Colton Idle

06/30/2021, 10:28 PM
If I comment out that line then it works again.
t

Tash

06/30/2021, 10:29 PM
What does the error say on AS ? (when you hover over
OutlineTextField
)
c

Colton Idle

06/30/2021, 10:30 PM
Restarting android studio did the trick. 😭
😅 1
Thank you everyone for checking my sanity.
🙏🏼 1
h

hfhbd

07/01/2021, 8:56 AM
Stupid question, is this approach preferred over a function/custom getter?
t

Tash

07/02/2021, 12:53 AM
Do you mean regarding usage of
derivedStateOf
?
h

hfhbd

07/02/2021, 2:15 AM
yes
m

Mitchell Skaggs

07/02/2021, 2:16 AM
Yes,
derivedStateOf
allows the Compose runtime to understand dependent functions and only recompute values if necessary.
h

hfhbd

07/02/2021, 10:00 AM
Good to know