How to build a new Compose screen with state hoisting
언제 쓸까: New screen in a Compose app — get it right the first time rather than refactoring later.
사전 조건
- Android Studio with a recent Compose BOM — Use the latest stable Compose BOM in your build
- Skill cloned — git clone https://github.com/anhvt52/jetpack-compose-skills ~/.claude/skills/jetpack-compose-skills
흐름
-
Describe the screenBuild a Profile screen: avatar, name, bio, follow button. State comes from a ProfileViewModel.✓ 복사됨→ Stateless composable + stateful wrapper + preview
-
Wire the ViewModelHook up the ViewModel using collectAsStateWithLifecycle.✓ 복사됨→ Lifecycle-aware state collection
-
Add a previewAdd @Preview variants for empty, loaded, and error states.✓ 복사됨→ Three previews rendering without a live ViewModel
결과: A clean Compose screen with previews and a testable structure.
함정
- Stateful composables you can't preview — Keep the composable stateless; hoist state to a wrapper
- Wrong Modifier order — Skill prompts the order rule — padding before background for a padded background, etc.