Should I prefer using method references for callba...
# compose
p
Should I prefer using method references for callbacks, to avoid unnecessary recomposition? Does maybe compiler automatically convert it to reference?
Copy code
Button(onClick = { vm.onClick() })
// vs
Button(onClick = vm::onClick)
j
AFAIK yes you should prefer those. Helped me with recompositions number of times already. Automatically converting it by the compiler plugin sounds like a great idea, but i'm not sure about feasibility here
a
A lambda won't be memorized if it captures an unstable value, and view models are usually unstable. (Stable is defined here)
And no, method references and lambdas are not equal. This post explains the difference. Tl;dr:
To sum up, the difference between lambda and method reference is that when we use method reference, the instance of variable, the method of which we use, is fixed at the moment the reference is created, not when it is called, unlike what happens when lambda is used.
💯 1