If i have to use a state inside a lambda , do i al...
# compose
p
If i have to use a state inside a lambda , do i always need to wrap it inside rememberUpdatedState? Seems like too much but it dosen’t work properly without it and it is a hit and miss. Am i missing something? Ex:-
Copy code
val image = rememberUpdatedState(mcqData.imageUrl)
GlideImage(imageModel = { image.value })
Or
Copy code
val screenModeUpdated by rememberUpdatedState(screenMode)
LaunchedEffect(key1) {
    if (screenModeUpdated == ScreenMode.SELECTION_MODEL)
        viewModel.initMcqs(
}
m
is GlideImage a wrapper of yours ?
p
No , it is part of this library
Copy code
com.github.skydoves:landscapist-glide:2.1.9
m
i’m not familiar with that library. But since imageModel is a lambda and not a variable, it must certainly can be recomposed with a new value. What happens if you just use
mcqData.imageUrl
in the lambda ?
p
if i use mcqData.imageUrl in the lambda , it sometime loads and sometimes it dosen’t . I feel the reason being mcqData.imageUrl is in my VM and that takes some tome to populate data, and the lambda tries to load image using the old url and not the updated one.
m
not likely. Are you sure the urls are valid? If there is some problem and the image cannot be loaded. It might be your problem there
if you use rememberUpdatedState it works as intended right ?
p
I have checked them and they are valid , i even used Coil or Glide Compose(in alpha), and they work flawlessly
Yes with rememberUpdatedState it works
m
even if the url is not state, the lambda should recompose. Maybe the library is doing something weird. Leave the rememberUpdatedState then. And maybe report a bug
well, it may be the case if only the url changes, compose will skip everything and not detect the change since the value is captured in the lambda. just use rememberUpdatedState and you will be fine
p
But what is looks like every time we use a State inside a lambda ,We should always wrap it inside rememberUpdatedState or else it can pick the old value.
s
for
image.value
, how is that
value
written at the place where it’s created. Is it
val value by mutableStateOf()
?
p
It is inside my a data class in my VM as StateFlow , and in compose am using it using collectAsStateWithLifecycle().