Skip to content

reactify

Category
Export Size
174 B
Last Changed
15 minutes ago
Alias
createReactiveFn
Related

将普通函数转换为响应式函数。转换后的函数接受引用作为其参数,并返回一个 ComputedRef,具有正确的类型。

TIP

想要看一些应用示例或者寻找一些预先已转换为响应式的函数吗?

查看 ⚗️ Vue Chemistry!

用法

基本示例

ts
import { reactify } from '@vueuse/core'

// 一个普通函数
function add(a: number, b: number): number {
  return a + b
}

// 现在它接受引用并返回一个计算引用
// (a: number | Ref<number>, b: number | Ref<number>) => ComputedRef<number>
const reactiveAdd = reactify(add)

const a = ref(1)
const b = ref(2)
const sum = reactiveAdd(a, b)

console.log(sum.value) // 3

a.value = 5

console.log(sum.value) // 7
js
import { reactify } from '@vueuse/core'
// 一个普通函数
function add(a, b) {
  return a + b
}
// 现在它接受引用并返回一个计算引用
// (a: number | Ref<number>, b: number | Ref<number>) => ComputedRef<number>
const reactiveAdd = reactify(add)
const a = ref(1)
const b = ref(2)
const sum = reactiveAdd(a, b)
console.log(sum.value) // 3
a.value = 5
console.log(sum.value) // 7

实现响应式 勾股定理 的示例。

ts
import { reactify } from '@vueuse/core'

const pow = reactify(Math.pow)
const sqrt = reactify(Math.sqrt)
const add = reactify((a: number, b: number) => a + b)

const a = ref(3)
const b = ref(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
js
import { reactify } from '@vueuse/core'
const pow = reactify(Math.pow)
const sqrt = reactify(Math.sqrt)
const add = reactify((a, b) => a + b)
const a = ref(3)
const b = ref(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

你也可以这样做:

ts
import { reactify } from '@vueuse/core'

function pythagorean(a: number, b: number) {
  return Math.sqrt(a ** 2 + b ** 2)
}

const a = ref(3)
const b = ref(4)

const c = reactify(pythagorean)(a, b)
console.log(c.value) // 5
js
import { reactify } from '@vueuse/core'
function pythagorean(a, b) {
  return Math.sqrt(a ** 2 + b ** 2)
}
const a = ref(3)
const b = ref(4)
const c = reactify(pythagorean)(a, b)
console.log(c.value) // 5

另一个将 stringify 变为响应式的示例

ts
import { reactify } from '@vueuse/core'

const stringify = reactify(JSON.stringify)

const obj = ref(42)
const dumped = stringify(obj)

console.log(dumped.value) // '42'

obj.value = { foo: 'bar' }

console.log(dumped.value) // '{"foo":"bar"}'

类型声明

typescript
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 interface ReactifyOptions<T extends boolean> {
  /**
   * Accept passing a function as a reactive getter
   *
   * @default true
   */
  computedGetter?: T
}
/**
 * 将普通函数转换为响应式函数。
 * 转换后的函数接受 ref 作为其参数,并返回一个具有正确类型的 ComputedRef。
 *
 * @param fn - 源函数
 */
export declare function reactify<T extends Function, K extends boolean = true>(
  fn: T,
  options?: ReactifyOptions<K>,
): Reactified<T, K>
export { reactify as createReactiveFn }

Source

SourceDocs

贡献者

Anthony Fu
丶远方
Anthony Fu
ordago

变更日志

v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue

Released under the MIT License.