|
3 | 3 | * @author Surmon <https://github.com/surmon-china>
|
4 | 4 | */
|
5 | 5 |
|
6 |
| -import { useState as useReactState } from 'react'; |
7 |
| -import { useWatch } from './watch'; |
8 |
| -import { useForceUpdate } from './utils'; |
| 6 | +import { useState as useReactState } from 'react' |
| 7 | +import { useWatch } from './watch' |
| 8 | +import { useForceUpdate } from './utils' |
9 | 9 | import {
|
10 | 10 | ref as vRef,
|
11 | 11 | shallowRef as vShallowRef,
|
12 | 12 | reactive as vReactive,
|
13 | 13 | shallowReactive as vShallowReactive,
|
14 | 14 | computed as vComputed,
|
15 | 15 | ComputedGetter,
|
16 |
| -} from './reactivity'; |
| 16 | +} from './reactivity' |
17 | 17 |
|
18 | 18 | /** ref hook */
|
19 | 19 | export function useRef<T>(initValue: T) {
|
20 |
| - const [value] = useReactState(() => vRef(initValue)); |
21 |
| - const forceUpdate = useForceUpdate(); |
22 |
| - useWatch(value, forceUpdate); |
23 |
| - return value; |
| 20 | + const [value] = useReactState(() => vRef(initValue)) |
| 21 | + const forceUpdate = useForceUpdate() |
| 22 | + useWatch(value, forceUpdate) |
| 23 | + return value |
24 | 24 | }
|
25 | 25 |
|
26 | 26 | /** shallowRef hook */
|
27 | 27 | export function useShallowRef<T extends object>(initValue: T) {
|
28 |
| - const [value] = useReactState(() => vShallowRef(initValue)); |
29 |
| - const forceUpdate = useForceUpdate(); |
30 |
| - useWatch(value, forceUpdate); |
31 |
| - return value; |
| 28 | + const [value] = useReactState(() => vShallowRef(initValue)) |
| 29 | + const forceUpdate = useForceUpdate() |
| 30 | + useWatch(value, forceUpdate) |
| 31 | + return value |
32 | 32 | }
|
33 | 33 |
|
34 | 34 | /** reactive hook */
|
35 | 35 | export function useReactive<T extends object>(target: T) {
|
36 |
| - const [value] = useReactState(() => vReactive(target)); |
37 |
| - const forceUpdate = useForceUpdate(); |
38 |
| - useWatch(value, forceUpdate); |
39 |
| - return value; |
| 36 | + const [value] = useReactState(() => vReactive(target)) |
| 37 | + const forceUpdate = useForceUpdate() |
| 38 | + useWatch(value, forceUpdate) |
| 39 | + return value |
40 | 40 | }
|
41 | 41 |
|
42 | 42 | /** shallowReactive hook */
|
43 | 43 | export function useShallowReactive<T extends object>(target: T) {
|
44 |
| - const [value] = useReactState(() => vShallowReactive(target)); |
45 |
| - const forceUpdate = useForceUpdate(); |
46 |
| - useWatch(value, forceUpdate); |
47 |
| - return value; |
| 44 | + const [value] = useReactState(() => vShallowReactive(target)) |
| 45 | + const forceUpdate = useForceUpdate() |
| 46 | + useWatch(value, forceUpdate) |
| 47 | + return value |
48 | 48 | }
|
49 | 49 |
|
50 | 50 | /** computed hook */
|
51 | 51 | export function useComputed<T>(getter: ComputedGetter<T>) {
|
52 |
| - const [value] = useReactState(() => vComputed(getter)); |
53 |
| - const forceUpdate = useForceUpdate(); |
54 |
| - useWatch(value, forceUpdate); |
55 |
| - return value; |
| 52 | + const [value] = useReactState(() => vComputed(getter)) |
| 53 | + const forceUpdate = useForceUpdate() |
| 54 | + useWatch(value, forceUpdate) |
| 55 | + return value |
56 | 56 | }
|
57 | 57 |
|
58 | 58 | /** any data to reactivity */
|
59 |
| -type ReactivityHookFn<T> = () => T; |
60 |
| -export function useReactivity<T = any>(getter: ReactivityHookFn<T>) { |
61 |
| - const forceUpdate = useForceUpdate(); |
62 |
| - // deep > watch > traverse(getter()) > ref | array | set | map | plain object(reactive) > forch update |
63 |
| - useWatch(() => getter(), forceUpdate, { deep: true }); |
64 |
| - return getter(); |
| 59 | +export function useReactivity<T = any>(getter: () => T) { |
| 60 | + const forceUpdate = useForceUpdate() |
| 61 | + // deep > watch > traverse(getter()) > ref | array | set | map | plain object(reactive) > force update |
| 62 | + useWatch(() => getter(), forceUpdate, { deep: true }) |
| 63 | + return getter() |
65 | 64 | }
|
0 commit comments