Problem Description
I'm creating an abstraction for my compose functions that use my custom view:
@Composable
fun DisposeBanner(bannerView: BannerView) {
DisposableEffect(key1 = true) {
onDispose {
bannerView.destroyAd()
}
}
}
However, as this function doesn't actually render any components I didn't want it to stick with the @Composable markup, but rather to indicate that it can only be used in compose functions through an extension.
My failed attempt
When trying to do:
fun Composable.disposeBanner(bannerView: BannerView) {
DisposableEffect(key1 = true) {
onDispose {
bannerView.destroyAd()
}
}
}
I get an error saying: "@Composable invocations can only happen from the context of a @Composable function"
Is it possible to do what I want?
CodePudding user response:
Composable is a function. You cannot do extensions on functions. And the error clearly states that you are trying to use composable DisposableEffect
function (which is a normal composable function that do not render any ui) in non-composable function.
You are overthinking this. There are a lot of composables that do not render any UI. @Composable
annotation is NOT bound to UI in any way. So doing a composable function that do not render anything is totally and completely fine