コンテンツにスキップ

reactify

カテゴリ
エクスポートサイズ
174 バイト
最終変更日
先週
エイリアス
createReactiveFn
関連

普通の関数をリアクティブ関数に変換します。変換された関数は引数としてrefを受け取り、適切な型付けでComputedRefを返します。

ヒント

アプリケーション例を見たいですか?または、事前にリアクティブ化された関数をお探しですか?

⚗️ Vue Chemistryをご覧ください!

使用方法

基本的な例

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

// 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 = 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'
// 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 = 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
}
/**
 * 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
ordago

変更履歴

v12.0.0-beta.1 2024/11/21
0a9ed - feat!: Vue 2 サポートの削除、バンドルの最適化、クリーンアップ (#4349)
v10.0.0-beta.4 2023/4/13
4d757 - feat(types)!: `MaybeComputedRef` から `MaybeRefOrGetter` に名前変更
0a72b - feat(toValue): `resolveUnref` から `toValue` に名前変更

MITライセンスでリリースされています。