Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ModifierComposable rule #395

Merged
merged 1 commit into from
Dec 10, 2024
Merged

Conversation

mrmans0n
Copy link
Owner

From the issue raised:

I believe this lint rule is out of date and should be removed. @composable Modifier extension functions used to be recommended against, but official documentation is currently more supportive of it: https://developer.android.com/develop/ui/compose/custom-modifiers#create_a_custom_modifier_using_a_composable_modifier_factory

@composable modifier extension functions help avoid unnecessary recompositions, compared to if you hadn't put the @composable annotation, because @composable auto-remembers lambdas from previous calls. This allows the Modifier to be considered equivalent to the previous value, and then doesn't prevent skipping because of it.

// This will always create a new Modifier that will prevent skipping, because the lambda inside is recreated on each call.
fun Modifier.myBackground(color: Color): Modifier = drawBehind { drawRect(color) }
vs

// This will return an equivalent Modifier because the lambda is automatically remembered from the previous call.
@composable
fun Modifier.myBackground(color: Color): Modifier = drawBehind { drawRect(color) }
I think a more appropriate lint would be against Modifier extension functions without the @composable annotation and could be recreating lambdas inside.

Addresses #393

@mrmans0n mrmans0n merged commit 3a7f3cd into main Dec 10, 2024
2 checks passed
@mrmans0n mrmans0n deleted the nacho/modifiercomposable-buhbye branch December 10, 2024 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant