https://kotlinlang.org logo
Title
f

Francis Mariano

04/04/2023, 2:37 PM
Hello, I have two approach to implement components with compose, what is the better?
@Composable
internal fun BasicContent(component: BasicComponent) {
    val state by component.states.collectAsState()

    OtherComposable(state.title, onClick = component::onClicked) // approach 1
    OtherComposableWithComponent(component)  // approach2
    OtherComposableWithState(state)  // approach3
}
Is it wrong use the second and third approach???
a

Arkadii Ivanov

04/04/2023, 3:04 PM
Every option may be fine. If your composable requires most of the component's or state's things, then you can pass as a whole. But the first approach may be cleaner in terms of Interface Segregation Principle (no code should depend on methods or properties it doesn't use).
s

s3rius

04/04/2023, 9:31 PM
Note that approach2 is very likely to turn
OtherComposableWithComponent
into a non-skippable function because
component
is probably unstable. This can cause performance issues. I noticed the same when I investigated why an animated transition between two components lagged, and found out that both UIs re-rendered on each frame because of that. I started adopting approach 1.