From 86d270a65d10a8d1eb95e5a701c603eb508ff946 Mon Sep 17 00:00:00 2001 From: ZL Asica <40444637+ZL-Asica@users.noreply.github.com> Date: Wed, 20 Nov 2024 21:46:04 -0600 Subject: [PATCH] fix: fix bugs for debounce and eventlistener --- .github/workflows/publish.yml | 2 +- CHANGELOG.md | 6 ++++++ jsr.json | 2 +- package.json | 2 +- src/hooks/dom/useEventListener.ts | 5 ++--- src/hooks/state/useDebouncedCallback.ts | 6 ++++-- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index a887549..a8daec0 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -65,7 +65,7 @@ jobs: # Step 8: Publish to JSR - name: 🌟 Publish to JSR - run: npx jsr publish --allow-dirty + run: pnpm dlx jsr publish --allow-dirty deploy-docs: runs-on: ubuntu-latest diff --git a/CHANGELOG.md b/CHANGELOG.md index 0afb0e8..fd2e036 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # @zl-asica/react +## 0.3.4 + +### Patch Changes + +- Minor bugs fixed for debounce and eventlistener + ## 0.3.3 ### Patch Changes diff --git a/jsr.json b/jsr.json index c2b33c6..2c404b9 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@zl-asica/react", - "version": "0.3.3", + "version": "0.3.4", "license": "MIT", "exports": "./src/index.ts", "importMap": "./import_map.json", diff --git a/package.json b/package.json index df0ec4d..8b27948 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@zl-asica/react", - "version": "0.3.3", + "version": "0.3.4", "description": "A library of reusable React hooks, components, and utilities built by ZL Asica.", "keywords": [ "react", diff --git a/src/hooks/dom/useEventListener.ts b/src/hooks/dom/useEventListener.ts index bf45d00..57e3b82 100644 --- a/src/hooks/dom/useEventListener.ts +++ b/src/hooks/dom/useEventListener.ts @@ -52,7 +52,6 @@ import { useDebouncedCallback } from '@/hooks/state'; * }; * ``` */ - export const useEventListener = ( event: string, handler: (event: T) => void, @@ -68,10 +67,10 @@ export const useEventListener = ( }, [handler, debounce]); useEffect(() => { - if (!element || !debouncedCallbackReference.current) return; + if (!element) return; const eventHandler = (event_: Event) => - debouncedCallbackReference.current!(event_ as T); // Type cast to match generic event type + debouncedCallbackReference.current?.(event_ as T); // Safe access with optional chaining element.addEventListener(event, eventHandler); return () => { diff --git a/src/hooks/state/useDebouncedCallback.ts b/src/hooks/state/useDebouncedCallback.ts index 56d6341..7328154 100644 --- a/src/hooks/state/useDebouncedCallback.ts +++ b/src/hooks/state/useDebouncedCallback.ts @@ -45,8 +45,10 @@ export const useDebouncedCallback = ( const callbackReference = useRef(callback); const timeoutReference = useRef(null); - // Always update the ref to the latest callback - callbackReference.current = callback; + useEffect(() => { + // Always update the ref to the latest callback + callbackReference.current = callback; + }, [callback]); const debouncedCallback = useCallback( (...arguments_: TArguments) => {