Colton Idle
04/11/2022, 9:40 PMMyTextField(
text = vm.state.email,
onTextChange = vm::emailChange,
...
}
but then emailChange(text: String)
basically does nothing except for setting the value. Better off just doing
MyTextField(
text = vm.state.email,
onTextChange = {vm.state.email = it},
I've always "thought" that all logic that can be moved up to a VM should be moved out of the UI layer, but after reading the arch docs im kinda convinced that whether or not i put stuff in a composable or a VM that's both the UI layer, and it basically "doesn't matter" where I put this stuff. Anyway. Thoughts?Landry Norris
04/11/2022, 9:42 PMFrancesc
04/12/2022, 1:38 AMonTextChange
instead of passing the viewmodel reference, you could be triggering recomposition each timeFrancesc
04/12/2022, 1:39 AMstate
should only be updated by the viewmodel, now you have 2 entities (the viewmodel and the view) updating it separatelyFrancesc
04/12/2022, 1:43 AMonClick
. This assumes the viewmodel does not want to do any validation on the value as it is updated and only wants the final value, but for an email field, I would presume you want to do some validation as it's been typedFrancesc
04/12/2022, 1:47 AMNorbi
04/12/2022, 4:52 PMkeep in mind that if you are creating a lambda for the@Francesc could you explain this? Thanks.instead of passing the viewmodel reference, you could be triggering recomposition each timeonTextChange
Francesc
04/12/2022, 5:16 PMFrancesc
04/12/2022, 5:17 PMNorbi
04/12/2022, 7:36 PMBy creating a new lambda each time the arguments are different and can trigger a recompositionI think that we have to investigate this deeper, in practice 😉 I have read in the Compose Internals book (although I don't understand it fully yet 😄 ) that the Compose compiler performs very clever optimizations regarding lambdas, even in case of capturing-lambdas. (If the lambda is non-capturing then the Kotlin compiler itself optimizes it into a singleton.) In the book the 'Compose lambda memoization' chapter describes more thoroughly how the Compose compiler replaces simple lambdas with
remember()
and synthetic function calls.
It seems to be very smart, so it is best to try before saying that recomposition will happen in a given case...Francesc
04/12/2022, 8:14 PMso it is best to try before saying that recomposition will happen in a given caseright, and that's why I prefaced my comment with
could
and noted I was not 100% sure - there are optimizations behind the hood