Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Follow publication

Understanding the various ref in Vue3: toRef, toRefs, isRef, unref, etc.

Shawn Kang
Stackademic
Published in
5 min readJan 27, 2024

--

Vue series of articles

In Vue3, there are many functions related to responsiveness, such as toRef, toRefs, isRef, unref, etc. Reasonably using these functions can greatly improve efficiency in actual development.

ref()

Everyone is no stranger to the ref API. It is often used in Vue3. Its function is to accept a value and return a responsive object. We can access and modify this value through the .value property. In the template, we can omit .value, for example in the following code, when the button is clicked, the count on the page will change responsively.

<template>
<div>
{{ count }}
<button @click="addCount">+1</button>
</div>
</template>

<script lang='ts' setup>
import { ref } from "vue"
const count = ref(1)
const addCount = () => {
count.value++
}
</script>

toRef

toRef can create a responsive ref based on a property in a responsive object. At the same time, this ref is synchronized with the property in the original object. If the value of…

--

--

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Written by Shawn Kang

Focusing on front-end development and internet writing. Sharing technical tutorials and original fiction. https://skstory.online/

No responses yet

Write a response