forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseState.ts
More file actions
31 lines (30 loc) · 978 Bytes
/
useState.ts
File metadata and controls
31 lines (30 loc) · 978 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { type ShallowRef, shallowRef } from "vue"
export function useState<T>(defaultValue: T): [
state: ShallowRef<T>,
setter: (val: T) => void,
reset: () => void,
]
export function useState<T>(defaultValue?: T): [
state: ShallowRef<T | undefined>,
setter: (val?: T) => void,
reset: () => void,
]
export function useState<T>(defaultValue?: T):
| [state: ShallowRef<T>, setter: (val: T) => void, reset: () => void]
| [state: ShallowRef<T | undefined>, setter: (val?: T) => void, reset: () => void] {
if (defaultValue === undefined || defaultValue === null) {
const result = shallowRef<T>()
return [
result,
(val?: T) => result.value = val,
() => result.value = undefined
]
} else {
const result = shallowRef<T>(defaultValue)
return [
result,
(val: T) => result.value = val,
() => result.value = defaultValue
]
}
}