reactify
将普通函数转换为 响应式函数。转换后的函数接受 ref 作为参数并返回一个 ComputedRef,并带有正确的类型定义。
对应用感兴趣或正在寻找预先响应式化的函数?请查看 ⚗️ Vue Chemistry!
使用
基础例子:
import { reactify } from '@vueuse/core'
import { shallowRef } from 'vue'
// a plain function
function add(a: number, b: number): number {
return a + b
}
// now it accept refs and returns a computed ref
// (a: number | Ref<number>, b: number | Ref<number>) => ComputedRef<number>
const reactiveAdd = reactify(add)
const a = shallowRef(1)
const b = shallowRef(2)
const sum = reactiveAdd(a, b)
console.log(sum.value) // 3
a.value = 5
console.log(sum.value) // 7
一个实现响应式 勾股定理 的例子。
import { reactify } from '@vueuse/core'
import { shallowRef } from 'vue'
const pow = reactify(Math.pow)
const sqrt = reactify(Math.sqrt)
const add = reactify((a: number, b: number) => a + b)
const a = shallowRef(3)
const b = shallowRef(4)
const c = sqrt(add(pow(a, 2), pow(b, 2)))
console.log(c.value) // 5
// 5:12:13
a.value = 5
b.value = 12
console.log(c.value) // 13
你也可以这样做:
import { reactify } from '@vueuse/core'
import { shallowRef } from 'vue'
function pythagorean(a: number, b: number) {
return Math.sqrt(a ** 2 + b ** 2)
}
const a = shallowRef(3)
const b = shallowRef(4)
const c = reactify(pythagorean)(a, b)
console.log(c.value) // 5
另一个实现响应式字符串化的例子。
import { reactify } from '@vueuse/core'
import { shallowRef } from 'vue'
const stringify = reactify(JSON.stringify)
const obj = shallowRef(42)
const dumped = stringify(obj)
console.log(dumped.value) // '42'
obj.value = { foo: 'bar' }
console.log(dumped.value) // '{"foo":"bar"}'
类型声明
export type Reactified<T, Computed extends boolean> = T extends (
...args: infer A
) => infer R
? (
...args: {
[K in keyof A]: Computed extends true
? MaybeRefOrGetter<A[K]>
: MaybeRef<A[K]>
}
) => ComputedRef<R>
: never
export type ReactifyReturn<
T extends AnyFn = AnyFn,
K extends boolean = true,
> = Reactified<T, K>
export interface ReactifyOptions<T extends boolean> {
/**
* Accept passing a function as a reactive getter
*
* @default true
*/
computedGetter?: T
}
/**
* Converts plain function into a reactive function.
* The converted function accepts refs as it's arguments
* and returns a ComputedRef, with proper typing.
*
* @param fn - Source function
* @param options - Options
*/
export declare function reactify<T extends AnyFn, K extends boolean = true>(
fn: T,
options?: ReactifyOptions<K>,
): ReactifyReturn<T, K>
export { reactify as createReactiveFn }