-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathuseCounter.ts
38 lines (35 loc) · 932 Bytes
/
useCounter.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
import { useCallback } from "react"
import { useDispatch, useSelector } from "react-redux"
import { CounterActions, countSelector } from "../store/counter"
type CounterOperators = {
count: number
increment: () => void
decrement: () => void
calculate: (inputNumber: number) => void
}
/**
* Counter custom-hooks
* @see https://reactjs.org/docs/hooks-custom.html
*/
export const useCounter = (): Readonly<CounterOperators> => {
const dispatch = useDispatch()
return {
count: useSelector(countSelector),
increment: useCallback(() => dispatch(CounterActions.increment()), [
dispatch,
]),
decrement: useCallback(() => dispatch(CounterActions.decrement()), [
dispatch,
]),
calculate: useCallback(
(inputNumber: number) => {
dispatch(
CounterActions.calculate({
inputNumber: inputNumber,
})
)
},
[dispatch]
),
}
}