Feel like I'm missing something basic. Anyone know...
# compose
c
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
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
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
All of my following composable fail after I add that line. Let me check the imports.
t
Seems to work ok…yea might be something to do with your env/file?
c
If I comment out that line then it works again.
t
What does the error say on AS ? (when you hover over
OutlineTextField
)
c
Restarting android studio did the trick. 😭
😅 1
Thank you everyone for checking my sanity.
🙏🏼 1
h
Stupid question, is this approach preferred over a function/custom getter?
t
Do you mean regarding usage of
derivedStateOf
?
h
yes
m
Yes,
derivedStateOf
allows the Compose runtime to understand dependent functions and only recompute values if necessary.
h
Good to know