-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathuseThinOut.ts
54 lines (49 loc) · 1.27 KB
/
useThinOut.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
import { useCallback } from "react"
import { useDispatch, useSelector } from "react-redux"
import {
IReduxSagaState,
ReduxSagaActions,
reduxSagaDebounceSelector,
reduxSagaThrottleSelector,
} from "../store/redux-saga"
type ThinOutOperators = {
debounce: (inputValue: string) => void
debounceState: IReduxSagaState
throttle: (inputValue: string) => void
throttleState: IReduxSagaState
}
/**
* Debounce and throttle custom-hooks
* @see https://reactjs.org/docs/hooks-custom.html
*/
export const useThinOut = (): Readonly<ThinOutOperators> => {
const dispatch = useDispatch()
const reduxSagaDebounceState = useSelector(reduxSagaDebounceSelector)
const reduxSagaThrottleState = useSelector(reduxSagaThrottleSelector)
const handleDebounce = useCallback(
(inputValue: string) => {
dispatch(
ReduxSagaActions.fetchDebounce({
input: inputValue,
})
)
},
[dispatch]
)
const handleThrottle = useCallback(
(inputValue: string) => {
dispatch(
ReduxSagaActions.fetchThrottle({
input: inputValue,
})
)
},
[dispatch]
)
return {
debounce: handleDebounce,
debounceState: reduxSagaDebounceState,
throttle: handleThrottle,
throttleState: reduxSagaThrottleState,
}
}