packages/shared/toRef/index.md
Normalize value/ref/getter to ref or computed.
import { toRef } from '@vueuse/core'
const foo = ref('hi')
const a = toRef(0) // Ref<number>
const b = toRef(foo) // Ref<string>
const c = toRef(() => 'hi') // ComputedRef<string>
toRefVueUse's toRef is not the same as Vue’s toRef from the vue package.
toRefobject + keytoRef| Behavior | VueUse toRef | Vue toRef |
|---|---|---|
| Accepts primitive values | ✔️ | ❌ |
| Accepts getter | ✔️ (computed) | ❌ |
| Accepts existing ref | ✔️ | ✔️ |
| Accepts object + key | ❌ | ✔️ |
| Writable | ✔️ (except getter) | ✔️ |
| Purpose | Normalize to ref/computed | Bind to reactive object |