跳到主要內容

reactify

類別
匯出大小
198 B
最後變更
5 天前
別名
createReactiveFn
相關

將純函式轉換為響應式函式。轉換後的函式接受 refs 作為參數,並返回一個 ComputedRef,並具有正確的類型。

提示

有興趣看看一些應用範例或尋找一些預先 reactify 的函式嗎?

查看 ⚗️ Vue Chemistry

用法

基本範例

ts
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
js
import { reactify } from '@vueuse/core'
import { shallowRef } from 'vue'
// a plain function
function add(a, b) {
  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

實作響應式畢氏定理的範例。

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

你也可以這樣做

ts
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
js
import { reactify } from '@vueuse/core'
import { shallowRef } from 'vue'
function pythagorean(a, b) {
  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

另一個製作響應式 stringify 的範例

ts
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"}'

類型宣告

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
}
/**
 * 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
 */
export declare function reactify<T extends Function, K extends boolean = true>(
  fn: T,
  options?: ReactifyOptions<K>,
): Reactified<T, K>
export { reactify as createReactiveFn }

原始碼

原始碼文件

貢獻者

Anthony Fu
Anthony Fu
IlyaL
James Garbutt
ordago

更新日誌

v12.8.0 於 2025/3/5
7432f - feat(types): 棄用 MaybeRef 和 MaybeRefOrGetter,改用 Vue 原生功能 (#4636)
v12.3.0 於 2025/1/2
59f75 - feat(toValue): 棄用 @vueuse/shared 中的 toValue,改用 Vue 原生功能
v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: 移除 Vue 2 支援,最佳化 bundles 並清理 (#4349)
v10.0.0-beta.4 於 2023/4/13
4d757 - feat(types)!: 將 MaybeComputedRef 重新命名為 MaybeRefOrGetter
0a72b - feat(toValue): 將 resolveUnref 重新命名為 toValue

根據 MIT 許可證發布。