recomposition_examples icon indicating copy to clipboard operation
recomposition_examples copied to clipboard

Tip3 lambda usage

Open badoualy opened this issue 2 years ago • 0 comments

Hi,

In the tip3, I think there's a potential catch that can trigger unncessary recompositions. The declaration of the lambda at this line:

https://github.com/AidaIssayeva/recomposition_examples/blob/main/app/src/main/java/com/cupsofcode/recomposition_examples/Tip3.kt#L49

When Tip3 is recomposed, even if the CardInfo value doesn't change, a new lambda will be created and passed to GoodGreetingsTip3, which will cause a complete recomposition down to GoodInnerColumn.

This article is explaining why it happens and some solutions: https://proandroiddev.com/understanding-re-composition-in-jetpack-compose-with-a-case-study-9e7d96d98095

This isn't exactly the topic of the tip, but since this is a very common case, I think it would be worth to fix it.

The easy fix: GoodGreetingsTip3(cardInfo::value)

Edit: might also be worth to add a comment to explain this

badoualy avatar Jun 03 '22 20:06 badoualy