Hello everyone :wave: Simple question: Is it ok to...
# compose
h
Hello everyone đŸ‘‹ Simple question: Is it ok to have a
LaunchedEffect
inside of Modifier extension?
Copy code
private fun Modifier.myModifier(): Modifier = composed {
    val shape = // ...
    LaunchedEffect(condition) {
        // ...
    }

    this then background(shape = shape)
}
Copy code
Context: Detekt is complaining about the use of `composed` or @Composable on Modifier extensions. So we need to add a Suppress, and our goal is to remove the composed or @Composable notation.
e
but depending on what you're doing, a custom
Modifier.Node
may be better. you'll have a coroutine scope to use too
h
I tried using a
Modifier.Node
, but I couldn't use
LaunchedEffect
inside of it
e
it has
coroutineScope
onAttach
and
onDetach
h
hm, but where do I place my
LaunchedEffect
block?
onAttach and onDetach don't have a composable scope
e
you launch it into the scope and cancel it
h
or do I replace LaunchedEffect by onAttach?
e
or use a side effect, depending on what you're doing
h
image.png
I'm not understanding how to use the side effect inside of the Modifier.Node
l
The logic you previously had inside launched effect can go inside that launch there
Copy code
LaunchedEffect() { suspendFoo() }
Becomes
Copy code
scope.launch { suspendFoo() }
Inside onAttach
e
I'd probably do
Copy code
var job: Job? = null
fun onAttach() {
    job?.cancel()
    job = coroutineScope.launch { ... }
}
fun onDetach() {
    job?.cancel()
    job = null
}
for more completeness
l
The scope will automatically be cancelled on attach and detach anyway, but yeah that’s also fine
h
hm... Got it
Thank you guys