@@ -60,15 +60,15 @@ Using this library, you do not have to concern yourself with how to construct, r
60
60
61
61
``` js
62
62
// useCounter.js
63
- import { useState } from ' react'
63
+ import { useState , useCallback } from ' react'
64
64
65
- function useCounter (initialCount = 0 ) {
66
- const [count , setCount ] = useState (initialCount )
65
+ function useCounter () {
66
+ const [count , setCount ] = useState (0 )
67
67
68
- const incrementBy = useCallback ((n ) => setCount (count + n ), [count])
69
- const decrementBy = useCallback ((n ) => setCount (count - n ), [count])
68
+ const increment = useCallback (() => setCount (count + 1 ), [count])
69
+ const decrement = useCallback (() => setCount (count - 1 ), [count])
70
70
71
- return { count, incrementBy, decrementBy }
71
+ return { count, increment, decrement }
72
72
}
73
73
74
74
export default useCounter
@@ -81,34 +81,20 @@ import useCounter from './useCounter'
81
81
82
82
afterEach (cleanup)
83
83
84
- test (' should create counter' , () => {
85
- const { result } = renderHook (() => useCounter ())
86
-
87
- expect (result .current .count ).toBe (0 )
88
- })
89
-
90
84
test (' should increment counter' , () => {
91
85
const { result } = renderHook (() => useCounter ())
92
86
93
- act (() => result .current .incrementBy ( 1 ))
87
+ act (() => result .current .increment ( ))
94
88
95
89
expect (result .current .count ).toBe (1 )
96
-
97
- act (() => result .current .incrementBy (2 ))
98
-
99
- expect (result .current .count ).toBe (3 )
100
90
})
101
91
102
92
test (' should decrement counter' , () => {
103
93
const { result } = renderHook (() => useCounter ())
104
94
105
- act (() => result .current .decrementBy ( 1 ))
95
+ act (() => result .current .decrement ( ))
106
96
107
97
expect (result .current .count ).toBe (- 1 )
108
-
109
- act (() => result .current .decrementBy (2 ))
110
-
111
- expect (result .current .count ).toBe (- 3 )
112
98
})
113
99
```
114
100
0 commit comments