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

Patrick Kellison

05/11/2022, 1:36 PM
Is there a clever way to do lazy initiation of variables within a composable scope?
e.g.
Copy code
@Composable
fun ComposableView(uri: String?){
     val uriHandler by lazy { LocalUriHandler.current }
     uri?.let { uriHandler.handleUri(uri) }
}
f

f.babic

05/11/2022, 1:38 PM
You can use a composition local where when you initialize the value for the 1st time, instead of throwing it, you set it internally. I guess that would be lazy-ish - but generally if there is a value that's compose-state-backed and it gets initialized after you start a composable scope, it shoudl trigger recomposition, so your scope has to refresh. You could also use side-effects for this with a bit of custom logic I guess
a

Albert Chang

05/11/2022, 2:57 PM
You can use
val someVal by remember { lazy { … } }
but your code snippet doesn’t make sense. What are you trying to do? Why do you want to create a lazy value of composition local?
75 Views