Back to Blog
ReactFormsComponentsPatterns

React Controlled vs Uncontrolled Components

Understand controlled and uncontrolled components in React. From form handling to refs to hybrid patterns.

B
Bootspring Team
Engineering
November 13, 2020
6 min read

Understanding the difference between controlled and uncontrolled components is essential for React forms. Here's a comprehensive guide.

Controlled Components#

Loading code block...

Uncontrolled Components#

Loading code block...

When to Use Each#

Loading code block...

Hybrid Approaches#

Loading code block...

Form Libraries#

Loading code block...

Complex Form Patterns#

Loading code block...

Comparison Table#

| Feature | Controlled | Uncontrolled | |------------------------|-----------------|-----------------| | Value source | React state | DOM | | Validation timing | Instant | On submit | | Input formatting | Yes | No | | Performance (many) | Can be slower | Better | | Code complexity | More | Less | | Third-party libs | May conflict | Easier | | File inputs | Not possible | Required | | Testing | Easier | Need refs |

Best Practices#

Controlled: ✓ Use for forms requiring validation ✓ Use for complex form logic ✓ Use for dependent fields ✓ Consider performance with many inputs Uncontrolled: ✓ Use for simple forms ✓ Use for file uploads ✓ Use with third-party libs ✓ Use defaultValue, not value General: ✓ Don't mix value and defaultValue ✓ Keep form state close to the form ✓ Consider form libraries for complex forms ✓ Use appropriate pattern for use case

Conclusion#

Controlled components offer more control for validation and formatting, while uncontrolled components are simpler and better for basic forms. Choose based on your needs: controlled for complex validation, uncontrolled for simple forms and file uploads. Consider hybrid approaches and form libraries for complex scenarios.

Share this article

Help spread the word about Bootspring

Related articles