Skip to content

Commit

Permalink
fix(hooks): useCounter template hook uses useCallback hook
Browse files Browse the repository at this point in the history
  • Loading branch information
heyitsarpit committed Dec 25, 2021
1 parent 1b0495d commit 3a7a952
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/core/_template/index.ts
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 }
}

0 comments on commit 3a7a952

Please sign in to comment.