Jorge Domínguez
09/16/2022, 9:37 PMval model = remember {
Model(
name = "John",
lastName = "Doe",
nationality = "Colombian",
type = Type.DEVELOPER
)
}
probably thinking about it too much but I'm not sure if re-instantiating the class on every recomposition has a significant costChris Fillmore
09/16/2022, 9:46 PMJorge Domínguez
09/16/2022, 10:15 PM@Composable
fun SomeComposable(
uiState: UiState
) {
val stateHolder = remember {
StateHolder(
title = uiState.title.format(),
subtitle = uiState.subtitle,
type = uiState.type
)
}
ChildComposable(stateHolder = stateHolder)
}
This is as simple as it can get but in reality there are several state holders, all of which depend on the uiState
, which comes from a ViewModel. that's also the reason why I'm not creating the state holders outside composition, because I need the reactivity of viewModel.uiState.observeAsState()
uiState
, the reason for that is that I need some string and drawable resources and don't wanna end up with a composable with lots of argumentsremember
has any benefits over simply having
val stateHolder = StateHolder(
...
)
tad
09/17/2022, 12:04 AMstateHolder
would be recreated on every recompositionformat()
would be called each timeStateHolder
, I would just pass title
, subtitle
and type
directly to ChildComposable
. Right now, if any field in uiState
changes, then this function is called and either remember { StateHolder(...) }
or StateHolder
is called, which has a nonzero cost.
Personally, I don't think wrapping arguments in a type is worth it just to reduce the number of arguments to a composable function, and it can introduce subtle performance issues unless the wrapper type is itself @Immutable
or inferred as such, or it's wired up to the composition with @Stable
and MutableState
fields. It's much simpler to avoid fighting the system and just pass your arguments directly, even if there are like 30 of them (in which case you might consider breaking up the function).
So in short, it looks to me like the internal library isn't designed well for Compose, and I would raise that with your team.