Is there a clever way to do lazy initiation of var...
# compose
p
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
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
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?
106 Views