-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(hooks): useCounter template hook uses useCallback hook
- Loading branch information
1 parent
1b0495d
commit 3a7a952
Showing
1 changed file
with
10 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import { useState } from 'react' | ||
import { useCallback, useState } from 'react' | ||
|
||
export function useCounter(initialValue = 0) { | ||
const [count, setCount] = useState(initialValue) | ||
|
||
const inc = (by = 1) => setCount((c) => c + by) | ||
const dec = (by = 1) => setCount((c) => c - by) | ||
const inc = useCallback((by = 1) => setCount((c) => c + by), []) | ||
const dec = useCallback((by = 1) => setCount((c) => c - by), []) | ||
const get = () => count | ||
const set = (val: number) => setCount(val) | ||
const reset = (val = initialValue) => { | ||
initialValue = val | ||
return set(val) | ||
} | ||
const set = useCallback((val: number) => setCount(val), []) | ||
const reset = useCallback( | ||
(val = initialValue) => { | ||
return set(val) | ||
}, | ||
[initialValue, set] | ||
) | ||
|
||
return { count, inc, dec, get, set, reset } | ||
} |