In jetpack compose, when passing a modifier into my composable, should the modifier be applied only to the outer most view, or all of the subviews as well?
Here's a quick example:
fun SomeComposable(modifier: Modifier = Modifier) {
Column(modifier = modifier) {
Text(modifier = modifier.extraModifiersIfNeeded(), text = "Text")
Text(modifier = modifier.extraModifiersIfNeeded(), text = "Text")
Text(modifier = modifier.extraModifiersIfNeeded(), text = "Text")
}
}
I would initially assume this is incorrect, because any modifier passed into SomeComposable would also change all of the subviews.
Are there any documentations making this very clear? Can I please have a link to any documentations?
Thank you!
When you pass a modifier to a composable, it should be elevated to the highest level that makes sense for your use case. This is because you could have unintended side effects say when you apply padding to your example. The Column
will have the padding applied to its contents, but then there would be additional padding to each Text
composable.
This documentation says to "extract them to the highest level possible".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With