-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.ts
62 lines (46 loc) · 1.49 KB
/
hooks.ts
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { useEffect, useRef, useState, type Dispatch, type RefObject, type SetStateAction } from 'react'
export function use_rerender () {
const set_state = useState({ })[1]
return function rerender () {
set_state({ })
}
}
export function use_modal (): ModalController {
const [visible, set_visible] = useState(false)
return {
visible,
open () {
set_visible(true)
},
close () {
set_visible(false)
}
}
}
export interface ModalController {
visible: boolean
open(): void
close(): void
}
/** 类似 useState, 同时将 state 绑定到 ref 从而方便的获取其最新的值, 返回 [state, ref, set_state] */
export function use_ref_state <TState> (initial_state?: TState | (() => TState)): [
TState,
RefObject<TState>,
Dispatch<SetStateAction<TState>>
] {
let [state, _set_state] = useState(initial_state)
let ref = useRef(state)
return [state, ref, (value: TState) => { _set_state(ref.current = value) }]
}
/** 根据 html element 渲染的高度更新 height state */
export function use_height <TElement extends HTMLElement = HTMLElement> (initial_height: number): [
number, RefObject<TElement>
] {
let [height, set_height] = useState(initial_height)
let ref = useRef<TElement>(undefined)
useEffect(() => {
if (ref.current)
set_height(ref.current.clientHeight)
}, [ref.current])
return [height, ref]
}