Migration Strategy: Converting Between Approaches
What you'll learn: How to safely convert forms between controlled and uncontrolled patterns when refactoring existing React code.
The Refactoring Challenge
You've inherited a form with controlled inputs that's causing performance issues, or you need to add validation to an uncontrolled form. How do you migrate without breaking everything? The key is incremental conversion with isolated testing.
Converting Controlled → Uncontrolled
When moving from controlled to uncontrolled (often for performance):
- Remove state declarations for form values
- Add
defaultValueinstead ofvalueprops - Replace
onChangehandlers withrefaccess - Defer validation to submit time or use blur events
- Update submit handler to read from refs or
FormData
Test each field individually before moving to the next.
Converting Uncontrolled → Controlled
When adding real-time features to uncontrolled forms:
- Add state for each input you need to control
- Replace
defaultValuewithvalue(never have both!) - Add
onChangehandlers to update state - Extract ref reads and replace with state references
- Keep other fields uncontrolled until needed (partial migration is valid)
The Hybrid Approach
You don't need to convert everything at once. Control only what needs dynamic behavior:
- Email field: controlled (for real-time validation)
- Password field: uncontrolled (just submit-time check)
- File input: always uncontrolled (DOM requirement)
Key Takeaway: Migrate forms incrementally, test each field independently, and remember you can mix patterns within the same form—control only what truly needs reactive behavior.