In Vue 3 with the Composition API, we often use ref() and reactive() to create reactive state. Although they look similar at first glance, they actually have important differences in how they are used and how they behave.
This article will discuss:
- The difference between the concepts of ref() and reactive()
- When to use each one
- Real code examples
- Common pitfalls
- Works with primitives e.g string, number, boolean and object.
- Auto-unwrapping in templates
- Returns an object with a .value property
- Single counter
- Toggle states
- Loading indicators
- Single form input
- Only works with object e.g object, array, Map, Set
- Direct property access without .value
- Ideal for complex structured state like forms and nested data
- Uses Vue's Proxy-based reactivity system
- Form data (login, register, profile, etc.)
- API response objects
- Global or shared state (small scale)
- Nested objects and arrays
This table alone already answers most beginner confusion about when to use each one. I normally use ref() as I find it more flexible.
