| name | compose-ui |
| description | Best practices for building UI with Jetpack Compose, focusing on state hoisting, detailed performance optimizations, and theming. Use this when writing or refactoring Composable functions. |
Jetpack Compose Best Practices
Instructions
Follow these guidelines to create performant, reusable, and testable Composables.
1. State Hoisting (Unidirectional Data Flow)
Make Composables stateless whenever possible by moving state to the caller.
2. Modifiers
- Default Parameter: Always provide a
modifier: Modifier = Modifier as the first optional parameter.
- Application: Apply this
modifier to the root layout element of your Composable.
- Ordering matters:
padding().clickable() is different from clickable().padding(). Generally apply layout-affecting modifiers (like padding) after click listeners if you want the padding to be clickable.
3. Performance Optimization
4. Theming and Resources
- Use
MaterialTheme.colorScheme and MaterialTheme.typography instead of hardcoded colors or text styles.
- Organize simple UI components into specific files (e.g.,
DesignSystem.kt or Components.kt) if they are shared across features.
5. Previews
- Create a private preview function for every public Composable.
- Use
@Preview(showBackground = true) and include Light/Dark mode previews if applicable.
- Pass dummy data (static) to the stateless Composable for the preview.