ref() Create a Reactive Reference

Last updated: 2026-10-22

ref() Create a Reactive Reference

ref() wraps any value in a reactive object accessed through .value. It is the way to hold primitives and values you want to replace as a whole.

GroupReactivity: Core
KindAPI
ScopeComponent
Vue Version3.0+
SSR SupportYes

📝 Syntax

import { ref } from "vue"

const count = ref(0)
count.value++          // read/write via .value in script
template: {{ count }}  // auto-unwrapped in templates

⚙️ Arguments

valueThe initial value of any type; undefined when omitted

Returns:A reactive object (Ref) with a .value property

▶ Example

Edit the code below and see the live result:

Output:

The page shows 'Clicked 0 times'; each click increments the number.

💡 Key Points

⚠️ Warning In script code always use count.value — writing count++ rebinds the variable and breaks reactivity.
💡 Tip When a ref is returned from setup, templates auto-unwrap it: write {{ count }}, not {{ count.value }}.
ℹ️ Note Use ref when the value gets replaced wholesale; use reactive() for deeply reactive objects.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q Why is .value required?
A JavaScript cannot track reads/writes on primitives, so a wrapper with getter/setter captures access — that wrapper property is .value.
Q Do objects inside ref lose reactivity?
A No. ref(obj) deep-proxies the object internally, so mutations via .value.foo still trigger updates.
Q When should I use ref vs reactive?
A Prefer ref by default (any type, replaceable, keeps reactivity when destructured); reactive only suits objects and loses reactivity when destructured.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏