Vue.js 3.0 Tutorial

Vue.js 3.0 Component Advanced

Vue.js 3.0 Transitions & Animations

Vue.js 3.0 Reusable & Combinable

Vue.js 3.0 Advanced

Vue.js 3.0 Tools

Vue.js 3.0 Scale

Vue.js 3.0 Accessibility

Vue.js 3.0 Migrating from Vue2

Vue.js 3.0 Contribute Documentation

Vue.js 3.0 API References

Vue.js 3.0 Style Guide

Vue 3.0 Reactive API Refs

In Vue 3's Composition API, one of the key reactive primitives is the ref function. ref is used to create a reactive reference to a value.

Here's a basic usage of ref:

import { ref } from 'vue'

export default {
  setup() {
    const count = ref(0)

    return {
      count
    }
  }
}

In this example, count is a reactive reference to the value 0.

Within the setup method, you must access the value of ref by using .value:

export default {
  setup() {
    const count = ref(0)

    function increment() {
      count.value++
    }

    return {
      count,
      increment
    }
  }
}

However, when a ref is returned from setup and used within the template, Vue automatically unwraps it and you can use it like a normal value:

<template>
  <button @click="increment">{{ count }}</button>
</template>

In this example, you can see count being used directly in the template, even though it's a ref.

It's important to remember that ref creates a reactive reference, not a reactive value. The reference is what's reactive, not the value itself. This means that if you reassign the reference to another value, it won't update the original ref.

When you need to manage a primitive value (like a string, number, or boolean) or when you want a reactive property inside an object, ref is a great tool to use.

Remember that Vue's reactivity system is built around these concepts, so understanding how ref and reactive work is essential for effective Vue development.

  1. Introduction to Vue 3.0 ref for Reactive Data:

    • ref creates a reactive reference for primitive values.
    • Provides a reactive wrapper around the value.
    import { ref } from 'vue';
    
    const count = ref(0);
    
  2. Vue 3.0 ref vs reactive for State Management:

    • Use ref for primitive values (numbers, strings, etc.).
    • Use reactive for complex objects.
    import { ref, reactive } from 'vue';
    
    const count = ref(0);
    
    const state = reactive({
      message: 'Hello, Vue 3.0!',
    });
    
  3. Vue 3.0 Creating and Updating refs:

    • Create a ref with an initial value.
    • Update the value using the .value property.
    import { ref } from 'vue';
    
    const count = ref(0);
    
    // Update the value
    count.value++;
    
  4. Vue 3.0 Multiple refs in a Component:

    • Create and manage multiple refs in a component.
    import { ref } from 'vue';
    
    const count = ref(0);
    const message = ref('Hello, Vue 3.0!');
    
  5. Vue 3.0 Accessing the Value of a ref:

    • Access the value of a ref using the .value property.
    import { ref } from 'vue';
    
    const count = ref(0);
    
    // Access the value
    const currentCount = count.value;
    
  6. Vue 3.0 Reactive Data with the ref Function:

    • Use the ref function to create reactive references.
    import { ref } from 'vue';
    
    const count = ref(0);
    
  7. Vue 3.0 Modifying refs with the .value Property:

    • Modify the value of a ref using the .value property.
    import { ref } from 'vue';
    
    const count = ref(0);
    
    // Modify the value
    count.value = 10;
    
  8. Vue 3.0 Watching refs with the watch Function:

    • Watch changes to a ref using the watch function.
    import { ref, watch } from 'vue';
    
    const count = ref(0);
    
    watch(count, (newValue, oldValue) => {
      console.log(`Count changed from ${oldValue} to ${newValue}`);
    });
    
  9. Vue 3.0 ref Objects and Reactivity:

    • ref objects are reactive and trigger updates.
    • Changes to the value are automatically detected.
    import { ref } from 'vue';
    
    const count = ref(0);
    
    // Changes to count will trigger updates
    count.value++;
    
  10. Vue 3.0 Using refs in Templates:

    • Access the value of a ref directly in templates.
    <template>
      <p>{{ count }}</p>
    </template>
    
    <script>
    import { ref } from 'vue';
    
    export default {
      setup() {
        const count = ref(0);
        return {
          count,
        };
      },
    };
    </script>
    
  11. Vue 3.0 Handling Reactivity Caveats with ref:

    • Be cautious with reactivity caveats.
    • Avoid adding non-reactive properties after creation.
    import { ref } from 'vue';
    
    const obj = ref({
      count: 0,
    });
    
    // Incorrect: Adding a non-reactive property
    obj.value.newProperty = 'Not reactive';
    
  12. Vue 3.0 refs and TypeScript:

    • Enhance type safety using TypeScript with ref.
    import { ref, Ref } from 'vue';
    
    const count: Ref<number> = ref(0);