Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/reactify.md
1---2category: Reactivity3alias: createReactiveFn4---56# reactify78Converts plain functions into reactive functions. The converted function accepts refs as its arguments and returns a ComputedRef, with proper typing.910::: tip11Interested to see some application or looking for some pre-reactified functions?1213Check out [⚗️ Vue Chemistry](https://github.com/antfu/vue-chemistry)!14:::1516## Usage1718Basic example1920```ts21import { reactify } from '@vueuse/core'22import { shallowRef } from 'vue'2324// a plain function25function add(a: number, b: number): number {26return a + b27}2829// now it accept refs and returns a computed ref30// (a: number | Ref<number>, b: number | Ref<number>) => ComputedRef<number>31const reactiveAdd = reactify(add)3233const a = shallowRef(1)34const b = shallowRef(2)35const sum = reactiveAdd(a, b)3637console.log(sum.value) // 33839a.value = 54041console.log(sum.value) // 742```4344An example of implementing a reactive [Pythagorean theorem](https://en.wikipedia.org/wiki/Pythagorean_theorem).4546<!-- eslint-skip -->4748```ts49import { reactify } from '@vueuse/core'50import { shallowRef } from 'vue'5152const pow = reactify(Math.pow)53const sqrt = reactify(Math.sqrt)54const add = reactify((a: number, b: number) => a + b)5556const a = shallowRef(3)57const b = shallowRef(4)58const c = sqrt(add(pow(a, 2), pow(b, 2)))59console.log(c.value) // 56061// 5:12:1362a.value = 563b.value = 1264console.log(c.value) // 1365```6667You can also do it this way:6869```ts70import { reactify } from '@vueuse/core'71import { shallowRef } from 'vue'7273function pythagorean(a: number, b: number) {74return Math.sqrt(a ** 2 + b ** 2)75}7677const a = shallowRef(3)78const b = shallowRef(4)7980const c = reactify(pythagorean)(a, b)81console.log(c.value) // 582```8384Another example of making reactive `stringify`8586```ts87import { reactify } from '@vueuse/core'88import { shallowRef } from 'vue'8990const stringify = reactify(JSON.stringify)9192const obj = shallowRef(42)93const dumped = stringify(obj)9495console.log(dumped.value) // '42'9697obj.value = { foo: 'bar' }9899console.log(dumped.value) // '{"foo":"bar"}'100```101102## Type Declarations103104```ts105export type Reactified<T, Computed extends boolean> = T extends (106...args: infer A107) => infer R108? (109...args: {110[K in keyof A]: Computed extends true111? MaybeRefOrGetter<A[K]>112: MaybeRef<A[K]>113}114) => ComputedRef<R>115: never116export type ReactifyReturn<117T extends AnyFn = AnyFn,118K extends boolean = true,119> = Reactified<T, K>120export interface ReactifyOptions<T extends boolean> {121/**122* Accept passing a function as a reactive getter123*124* @default true125*/126computedGetter?: T127}128/**129* Converts plain function into a reactive function.130* The converted function accepts refs as it's arguments131* and returns a ComputedRef, with proper typing.132*133* @param fn - Source function134* @param options - Options135*136* @__NO_SIDE_EFFECTS__137*/138export declare function reactify<T extends AnyFn, K extends boolean = true>(139fn: T,140options?: ReactifyOptions<K>,141): ReactifyReturn<T, K>142/** @deprecated use `reactify` instead */143export declare const createReactiveFn: typeof reactify144```145