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.
Поток
-
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.