Jetpack Compose Form State: Why It's Hard & How to Choose
In this article
Jetpack Compose form state feels difficult because it forces you to rethink state management entirely—moving from imperative View-based patterns to a declarative, unidirectional data flow where every keystroke triggers recomposition. Choosing the right approach depends on your form's complexity, validation needs, and team familiarity. This article explains why it feels hard and gives you a practical framework for deciding between remember, ViewModel, and third-party libraries.
Why Traditional Form State Doesn't Translate
In the old View system, you'd grab a reference to an EditText, read its text property, and update it imperatively. Compose inverts this: the UI is a function of state, and you can't just "get" a field's value—you have to hoist it. This leads to a mental shift where every text field needs a value and an onValueChange callback. The difficulty isn't the API; it's that you're now managing state explicitly, and the framework gives you multiple tools for it, each with trade-offs.
Core State Tools in Compose
Compose offers several built-in state holders, each suited to different scopes:
remember: Stores state in composition, lost when the composable leaves the composition (e.g., screen rotation or navigation).rememberSaveable: Survives configuration changes and process death via aBundle, but only for types that can be saved (or with a custom Saver).ViewModel: Survives configuration changes and is scoped to a lifecycle owner (Activity, Fragment, or Navigation back stack entry). Often paired withStateFloworMutableState.- Third-party libraries (e.g.,
androidx.lifecycle.viewmodel.composeor community solutions likeDecompose): Add extra patterns like reducers or validation helpers.
For a simple form with a few fields and no validation, rememberSaveable is often enough. For anything with business logic, a ViewModel is the standard choice.
Why It Feels Hard: The Pain Points
Developers commonly struggle with these aspects:
- Recomposition and performance: Every keystroke updates state, causing recomposition. If you hold all fields in a single
data class, the whole form recomposes, which can be janky on low-end devices. - Validation timing: When do you validate? On each change? On submit? Showing errors too early is bad UX, but deferring them requires extra state.
- State duplication: You might have the text field's value and a separate error string, leading to inconsistent states.
- Lifecycle awareness: If you use
rememberwithout saving, rotation clears the form—a classic bug. - Keyboard and focus: Managing focus across fields (e.g., moving to next on IME action) requires explicit
FocusRequesterandFocusManager.
How to Choose an Approach
Here's a decision framework that works in practice:
- Form length and complexity: 1–3 fields, no validation? Use
rememberSaveable. More fields or any cross-field validation? Use aViewModel. - Validation requirements: If you need to validate on submit and show errors, a
ViewModelwith a state class (e.g.,FormStateholding values and errors) is cleaner. - Data persistence: Do you need the form to survive process death?
rememberSaveablecan handle simple values, but for complex objects, aViewModelwith a SavedStateHandle is more robust. - Team familiarity: If your team knows MVVM, stick with
ViewModel. Introducing a third-party library adds learning cost. - Testability:
ViewModelis easier to unit test than composable state.
For most production apps, the answer is a ViewModel that exposes a single StateFlow and functions like onFieldChange and submit. This keeps the UI dumb and logic testable.
Common Mistakes and Misconceptions
A frequent misconception is that you must use remember for everything. In fact, using remember for form fields that need to survive rotation is a bug. Another mistake is putting all fields in one mutableStateOf data class, causing unnecessary recompositions. Instead, consider using separate mutableStateOf for each field, or use derivedStateOf for computed values like "is form valid."
Another pitfall is ignoring focus. When you clear a field's value programmatically, the keyboard may stay open or the cursor may jump. You need to manage focus explicitly with FocusRequester and onFocusChanged modifiers.
Comparison: Built-in vs. Third-Party Libraries
Built-in tools (remember, ViewModel) are sufficient for most forms. Third-party libraries like FormValidator or Kotlin Validation can reduce boilerplate, but they add dependencies and often force a specific architecture. For example, a library might require you to use a particular state holder, conflicting with your existing ViewModel pattern. In general, stick to built-in tools unless you have a very complex form with dynamic fields or multi-step validation that warrants a dedicated solution.
Costs and Trade-offs
The cost of choosing the wrong approach is usually rework. A simple form that starts with remember will need to be refactored to a ViewModel once validation is added. The cost of using a ViewModel for a tiny form is extra boilerplate and a steeper learning curve for junior devs. There's also a performance cost: holding a large state object in a StateFlow and updating it on every keystroke can cause recomposition of the entire form. Mitigate by using flatMapLatest or by splitting state into smaller flows.
Frequently Asked Questions
Why does my form lose data when I rotate the screen?
If you used remember without rememberSaveable, the state is lost because the activity is recreated. Use rememberSaveable for simple values, or better, a ViewModel that survives configuration changes.
Should I use a ViewModel for every form?
Not necessarily. For a simple login form with two fields and no validation, rememberSaveable is fine. But if you have validation, async calls, or need to share data with other screens, a ViewModel is the standard choice.
How do I avoid recomposing the entire form on every keystroke?
Keep each field's state in its own mutableStateOf rather than a single data class. Also, use derivedStateOf for computed values like "is valid" so they only recompose when their inputs change.
What's the best way to validate a form in Compose?
Define a FormState data class that holds both values and error strings. Update errors in the ViewModel on field change or on submit. Use isError and supportingText parameters on OutlinedTextField to display errors.
Can I use LiveData with Compose forms?
Yes, you can use LiveData with observeAsState(), but StateFlow is more idiomatic for Compose because it's Kotlin-first and works seamlessly with collectAsState(). LiveData is fine if you're migrating an existing app.
Is it okay to use a third-party form library?
It's okay if the library fits your architecture and you're willing to add a dependency. But for most forms, built-in tools are sufficient and more maintainable. Evaluate the library's maintenance status and API stability before adopting.