From 3a7a9522aeac3ed34cf472bb1eee812233474a16 Mon Sep 17 00:00:00 2001 From: Arpit Bharti Date: Sun, 26 Dec 2021 00:56:07 +0530 Subject: [PATCH] fix(hooks): useCounter template hook uses useCallback hook --- packages/core/_template/index.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/core/_template/index.ts b/packages/core/_template/index.ts index 7033f88..18171b4 100644 --- a/packages/core/_template/index.ts +++ b/packages/core/_template/index.ts @@ -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 } }