Skip to Content

Learn To Use Vue 3 $refs With Vue Ref Examples

Are you ready to harness the full potential of Vue 3 $refs and unleash a whole new level of interactivity in your Vue applications?

You’re in the right place! In this tutorial, we’ll unveil the secrets of $refs and demonstrate how Vue ref can supercharge your development process. 

With the help of real-world examples, we’ll guide you through the ins and outs of $refs, from accessing DOM elements to communicating with child components.

Together, we’ll master the art of using $refs in Vue 3 and take your projects to the next level!

No products found.

If you want to learn more about How To Use Vue Props With Vue Prop Examples

Vue 3 $refs

Vue 3 $refs provides a way to directly access and manipulate DOM elements or components within a Vue instance.

What Is Vue 3 $refs?

What Is Vue 3 $refs? The $refs property is a feature introduced in Vue 3 that provides a way to directly access and manipulate DOM elements or components within a Vue instance.

It allows you to reference elements or components defined in your template and access their properties and methods programmatically.

The $refs property can be used in both the options API and the composition API.

When a component is rendered, Vue assigns a unique reference to each element or component that has a ref attribute. These references can then be accessed through the $refs property of the Vue instance.

It provides a convenient way to interact with specific elements or components without the need for complex event handling or traversing the component hierarchy.

Vue 3 $refs Composition API

Vue 3 $refs Composition API: The composition API in Vue 3 provides a set of functions that allow you to create reusable logic and composition functions within your components.

When it comes to $refs, the composition API allows you to access and manipulate the references using the ref function.

The ref function creates a reactive reference that can be accessed through the $refs property.

Here’s an example of how you can use the composition API with $refs in Vue 3:

Vue 3 $refs Composition API

In this example, the myButton ref is defined using the Composition API’s ref function within the setup function.

The ref is then used in the template as a reference to the button element.

When the button is clicked, the handleClick method is triggered, and the disabled property of the button is set to true.

Check out my article about Learn Migrating Vue 2 To Vue 3 With Vue Migration Examples.

Vue 3 $refs TypeScript

Vue 3 $refs TypeScript: With TypeScript, the $refs feature remains similar to its JavaScript counterpart, but TypeScript adds an additional layer of type safety and enables you to define explicit types for the $refs property.

This ensures that you have compile-time checks and helps prevent potential runtime errors when accessing and manipulating components or elements through $refs.

Let’s consider an example to illustrate the usage of $refs with TypeScript in a Vue 3 component. 

Vue 3 $refs TypeScript

In this example, the myInput ref is defined with the type HTMLInputElement | null.

This type indicates that the $refs property should refer to an input element or be null if the element is not found.

TypeScript ensures type safety, allowing you to access properties or methods specific to HTMLInputElement without any type errors.

Vue Ref

Vue ref Object

Vue ref Object: The ref function can be used to create a reactive reference to an object.

This allows you to define a variable that can be accessed and modified, with the changes automatically triggering reactivity within the component.

The ref object is a fundamental part of Vue’s reactivity system, enabling efficient and reactive data handling.

When you create a ref object in Vue, you provide an initial value that becomes the underlying value of the ref.

This value can be of any type, such as a string, number, boolean, or even a more complex object.

Vue wraps the initial value in a reactive proxy, allowing it to track changes to the value and update any component bindings that depend on it.

The key feature of a ref object is that you can access and modify its value using the .value property.

When you read the ref value, Vue ensures that the component is aware of the dependency and updates the necessary bindings in the template.

When you modify the ref value, Vue detects the change and triggers the reactivity mechanism, causing the component to update any affected parts of the UI.

For example, let’s say we have a Vue component that needs to keep track of a counter:

Here’s an example of using ref to create a reactive object:

Vue ref Object

In this example, the count variable is defined as a ref object with an initial value of 0. In the template, we can access the value of count using count.value.

When the “Increment” button is clicked, the increment method is triggered, and the value of count is incremented.

Vue ref Array

Vue ref Array: The ref function in Vue also allows you to create reactive references to arrays.

When creating a ref array, Vue ensures that any modifications to the array, such as adding or removing elements, trigger reactive updates in the component.

Here’s an example of using ref to create a reactive array:

Vue ref Array

In this example, the items variable is defined as a ref object containing an initial array of items. The v-for directive is used to render each item in the array dynamically.

When the “Add Item” button is clicked, the addItem method is triggered, and a new item is added to the items array.

The reactivity system ensures that the UI is updated automatically to reflect the changes.

Don´t forget to check out my article about How To Use Vue Js Header? (Vue Headers Examples)

Vue ref Function

Vue ref Function: The ref function can also be used to create a reactive reference to a function. This allows you to define and use reactive functions within your component’s logic.

Here’s an example of using ref to create a reactive function:

Vue ref Function

In this example, the message variable is defined as a ref object with an initial value of ‘Hello’.

The getMessage variable is also defined as a ref object, but instead of a value, it contains a reactive function that returns the value of message.

When the “Update Message” button is clicked, the updateMessage method is triggered, and the value of message is updated.

The reactivity system ensures that the getMessage function is re-evaluated, and the updated message is displayed in the template.

Vue ref In Slot

Vue ref In Slot: You can also use ref within slots to access and manipulate elements or components passed through the slot.

This allows you to interact with specific elements or components within the slot content from the parent component.

Here’s an example of using ref in a slot:

Vue ref In Slot

In this example, the slotContent ref is defined within the parent component. The ref is applied to the slot element, allowing you to access the content passed through the slot.

When the “Highlight Slot Content” button is clicked, the highlightSlotContent method is triggered, and the background color of the slot content is changed to yellow.

The ref in the slot enables direct manipulation of specific elements or components within the slot from the parent component.

Don´t forget to check out my article about How To Use Filters Vue 3

Vue 3 Script Setup Refs

Vue 3 Script Setup Refs: Vue 3 introduced the Composition API, which provides a more flexible and scalable way to organize and reuse logic in Vue components.

One of the features in the Composition API is the ability to use refs, which are reactive containers for storing mutable values.

Refs play a crucial role in Vue 3’s reactivity system, allowing you to track and update values efficiently.

Vue 3 Script Setup Template Ref

Vue 3 Script Setup Template Ref: You can use the ref function in the script setup section to create a reactive reference to a value.

This is particularly useful when you need to access DOM elements or other component instances directly.

Let’s take a look at an example to understand how to use the ref function in the template.

Vue 3 Script Setup Template Ref

In the example above, we have a simple Vue component with a <div> element. We use the ref function and assign it to the myElement variable.

The ref function takes an initial value as its argument, which in this case is set to null.

Inside the onMounted hook, we can access the value of myElement using the .value property. In this case, we log the value to the console, but you can perform any desired operations with it.

When the component is mounted, Vue will automatically assign the DOM element to the myElement ref.

This allows us to access and manipulate the DOM element within our component’s logic.

By using the ref function, the reactivity system of Vue will keep track of changes to the referenced value and trigger updates whenever necessary.

Vue 3 Composition API Ref Element

Vue 3 Composition API Ref Element Example

Vue 3 Composition API Ref Element Example: The Composition API in Vue 3 provides the ref function, which allows you to create reactive references to values.

This is particularly useful when you want to work with mutable values within your components.

Let’s explore an example that demonstrates the use of ref in the Composition API.

Vue 3 Composition API Ref Element Example

In the example above, we have a simple Vue component that displays a count value and a button.

We define a count variable using the ref function and initialize it with a value of 0. The ref function creates a reactive reference to the count value, allowing Vue to track changes to it.

Inside the component’s logic, we define an increment function that increments the count value by one when the button is clicked.

We access the count value using the .value property of the count ref.

Vue’s reactivity system ensures that the template is updated automatically whenever the count value changes.

By using the ref function in the Composition API, we can easily create reactive references to values and leverage Vue’s reactivity system to handle state changes efficiently. This enables us to build more dynamic and interactive components in Vue 3.

How Do I Access $refs In Composition API?

In Vue 3’s Composition API, accessing the $refs property is slightly different compared to the Options API.

Instead of directly accessing it from the component instance, you can use the toRef function to create a reactive reference to a $refs value.

Let’s dive into the details of accessing $refs in the Composition API.

To access the $refs property in the Composition API, you first need to import the toRef function from the Vue package.

Then, within your component’s logic, you can use the toRef function to create a reactive reference to a specific $refs value. Here’s an example to illustrate the usage:

How Do I Access $refs In Composition API?

In the example above, we have a <div> element with a ref attribute named “myElement.” In the script setup section, we import the toRef function from the Vue package.

We then use the toRef function to create a reactive reference, myElementRef, to the myElement ref on the component’s $refs object.

By accessing myElementRef.value, we can obtain the DOM element referenced by the myElement ref. Vue’s reactivity system will ensure that the myElementRef.value is updated whenever the referenced DOM element changes.

It’s important to note that when accessing $refs in the Composition API, the DOM elements referenced by $refs might not be immediately available when the component is mounted.

You can use lifecycle hooks like onMounted to ensure that you access the $refs values when they are ready.

How Do I Change The Ref Value In Vue 3?

Changing the value of a ref is straightforward. You can directly assign a new value to the .value property of the ref. Let’s explore the process of changing a ref value in Vue 3.

To change the value of a ref in Vue 3, you can simply assign a new value to the .value property of the ref. Here’s an example to illustrate the process:

How Do I Change The Ref Value In Vue 3?

In the example above, we have a Vue component that displays a count value and provides two buttons: “Increment” and “Reset.”

We define a count ref and initialize it with a value of 0. The count ref is used in the template by accessing the .value property.

The increment function increases the value of count by one when the “Increment” button is clicked. We achieve this by assigning count.value + 1 to count.value.

The reset function sets the value of count back to 0 when the “Reset” button is clicked. We accomplish this by directly assigning 0 to count.value.

By modifying the .value property of a ref, Vue’s reactivity system ensures that the template is updated accordingly.

This allows you to change the value of a ref dynamically and have the changes reflected in your component’s UI.

Hopefully, you now have a better understanding of how you can use Vue 3 $refs.

Understanding how you can use Vue 3 $refs is gonna provide a way to directly access and manipulate DOM elements or components within a Vue instance.