From 5ff073ae5af14b7a10797089db0716e60cc72758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Mon, 6 Feb 2023 13:49:00 +0100 Subject: [PATCH] fix: allow to call `useKbsDisableGlobal` outside of context --- src/component/KbsProvider.tsx | 4 ++++ src/component/hooks/useKbsDisableGlobal.ts | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/component/KbsProvider.tsx b/src/component/KbsProvider.tsx index 40c04e1..e60985e 100644 --- a/src/component/KbsProvider.tsx +++ b/src/component/KbsProvider.tsx @@ -43,6 +43,10 @@ export const kbsContext = createContext(initialKbsState); const kbsDispatchContext = createContext | null>(null); +export function useKbsUncheckedDispatch() { + return useContext(kbsDispatchContext); +} + export function useKbsDispatch() { const dispatch = useContext(kbsDispatchContext); if (!dispatch) { diff --git a/src/component/hooks/useKbsDisableGlobal.ts b/src/component/hooks/useKbsDisableGlobal.ts index 483704e..63706eb 100644 --- a/src/component/hooks/useKbsDisableGlobal.ts +++ b/src/component/hooks/useKbsDisableGlobal.ts @@ -1,11 +1,11 @@ import { useEffect } from 'react'; -import { useKbsDispatch } from '../KbsProvider'; +import { useKbsUncheckedDispatch } from '../KbsProvider'; export function useKbsDisableGlobal(disabled = true) { - const kbsDispatch = useKbsDispatch(); + const kbsDispatch = useKbsUncheckedDispatch(); useEffect(() => { - if (!disabled) return; + if (!disabled || !kbsDispatch) return; kbsDispatch({ type: 'DISABLE_GLOBAL' }); return () => kbsDispatch({ type: 'ENABLE_GLOBAL' }); }, [kbsDispatch, disabled]);