KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
🖖

Vue

Topic Hub & Articles

Vue Intro

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Get Started

10 min

Vue Directives

10 min

Vue Events

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Forms

10 min

Vue Components

10 min

Vue Props

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Methods

10 min

Vue Computed Properties

10 min

Vue Watchers

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Class and Style Bindings

10 min

Vue Conditional Rendering

10 min

Vue List Rendering

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Template Syntax

10 min

Vue Teleport

10 min

Vue Suspense

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Transitions

10 min

Vue Mixins

10 min

Vue Custom Directives

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Plugins

10 min

Vue Slots

10 min

Vue Provide / Inject

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Dynamic Components

10 min

Vue Async Components

10 min

Composition API Intro

10 min

Recap Quiz

Gated

Milestone Assessment

ref vs reactive

10 min

Lifecycle Hooks

10 min

Dependency Injection

10 min

Recap Quiz

Gated

Milestone Assessment

Computed and Watch

10 min

Template Refs

10 min

Composable Functions

10 min

Script Setup

10 min

Progress
0%

0 / 32 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
VueVue Composition API
Lesson

ref vs reactive

10 min reading
Free Course

ref vs reactive: Choosing Reactivity Primitives in Vue 3

Vue 3 gives you two main functions for creating reactive state in the Composition API: ref() and reactive(). Both wrap values in Vue's Proxy reactivity system, yet they follow different rules for reassignment, data types, and destructuring.

Picking the right primitive prevents silent reactivity bugs where a variable updates in JavaScript while the template stops refreshing.

flowchart TD
    Prim["Primitives or Reassignable Objects"] --> Ref["ref(val) -> .value Wrapper"]
    Obj["Grouped Form or State Objects"] --> React["reactive(obj) -> ES6 Proxy"]
    React -->|Destructured Directly| Break["Loses Reactivity"]
    React -->|With toRefs| Safe["Keeps Reactive Links"]

Comparing ref() and reactive()

Feature ref() reactive()
Value Types Primitives, arrays, and objects Objects, arrays, Map, Set only
Script Access Uses .value (count.value++) Direct properties (state.count++)
Template Access Auto-unwrapped ({{ count }}) Direct properties ({{ state.count }})
Full Reassignment Safe (user.value = nextUser) Breaks reactive connection

Practical SFC Comparison Example

Both primitives drive template updates automatically, yet each handles object replacement and destructuring differently. Notice how toRefs(userState) turns each property of a reactive object into a linked ref so destructuring stays reactive.

<script setup>
import { ref, reactive, toRefs } from 'vue';

const count = ref(0);
const userRef = ref({ name: 'Alex' });

const userState = reactive({
  name: 'Sarah',
  role: 'Developer'
});

function updateRef() {
  count.value++;
  userRef.value = { name: 'Jordan' };
}

function updateReactive() {
  userState.name = 'Taylor';
}

const { name, role } = toRefs(userState);
</script>

<template>
  <div class="reactivity-demo">
    <p>Ref Count: {{ count }} | User: {{ userRef.name }}</p>
    <p>Reactive Destructured: {{ name }} ({{ role }})</p>

    <button @click="updateRef">Update Ref</button>
    <button @click="updateReactive">Update Reactive</button>
  </div>
</template>

Common Mistakes

  • Passing Primitives to reactive(): Calling reactive(0) or reactive('hello') does not work because JavaScript Proxies only wrap objects. Use ref() for numbers, strings, and booleans.
  • Reassigning a Whole reactive() Variable: Writing userState = reactive({ name: 'New' }) disconnects the template from the new proxy. Either mutate userState.name or switch to ref().
  • Destructuring reactive() Without toRefs(): Writing const { name } = userState extracts a plain string snapshot that stops updating when userState.name changes.

Frequently Asked Questions

Should I default to ref() or reactive() in new Vue 3 code?

Vue's official documentation recommends defaulting to ref(). It works for every data type, allows replacing whole objects cleanly, and makes reactive variables obvious thanks to .value.

Does Vue unwrap nested ref values inside reactive()?

Yes. If you place const count = ref(0) inside const state = reactive({ count }), reading state.count in script returns 0 without needing .value.

When is shallowRef() better than ref()?

Use shallowRef() for large immutable datasets or third-party class instances where you only replace .value as a whole and do not need deep property tracking.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Related Developer Tools & Topic Hub

Vue Topic Hub
JSON Formatter
Validate and format JSON payloads
HTML Formatter
Format HTML templates
CSS Formatter
Clean and format stylesheets
All Vue Articles
Deep-dive guides, tips & discussions
PreviousComposition API Intro
Continue JourneyNext: Lifecycle Hooks

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum