Skip to content

reactify

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

¥Converts plain functions into reactive functions. The converted function accepts refs as its arguments and returns a ComputedRef, with proper typing.

提示

有兴趣查看一些应用或寻找一些预反应的函数吗?

¥Interested to see some application or looking for some pre-reactified functions?

看看 ⚗️ 视化学

¥Check out ⚗️ Vue Chemistry!

用法

¥Usage

基本示例

¥Basic example

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

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

¥An example of implementing a reactive Pythagorean theorem.

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

你也可以这样做:

¥You can also do it this way:

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 的另一个例子

¥Another example of making reactive 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"}'