From 6aa5c97ce2bba12ee6ed1eb68d94f308bc2deac0 Mon Sep 17 00:00:00 2001 From: Kai Vandivier Date: Tue, 12 Dec 2023 07:52:02 +0100 Subject: [PATCH] fix: reinstate undefined; remove checks --- .../use-online-status-message.test.tsx | 2 +- .../offline/src/lib/online-status-message.tsx | 27 ++++--------------- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/services/offline/src/lib/__tests__/use-online-status-message.test.tsx b/services/offline/src/lib/__tests__/use-online-status-message.test.tsx index e8e37f926..fe3d3ddac 100644 --- a/services/offline/src/lib/__tests__/use-online-status-message.test.tsx +++ b/services/offline/src/lib/__tests__/use-online-status-message.test.tsx @@ -16,7 +16,7 @@ describe('useOnlineStatusMessage', () => { wrapper, }) - expect(result.current.onlineStatusMessage).toBeNull() + expect(result.current.onlineStatusMessage).toBeUndefined() act(() => { result.current.setOnlineStatusMessage('8 offline events') diff --git a/services/offline/src/lib/online-status-message.tsx b/services/offline/src/lib/online-status-message.tsx index d9f690d2f..726b41a5e 100644 --- a/services/offline/src/lib/online-status-message.tsx +++ b/services/offline/src/lib/online-status-message.tsx @@ -4,7 +4,8 @@ type SetOnlineStatusMessage = (message: ReactNode) => void // 'get' and 'set' contexts are separated so 'setter' consumers that don't // actually need the value don't have to rerender when the value changes: -const OnlineStatusMessageValueContext = React.createContext(null) +const OnlineStatusMessageValueContext = + React.createContext(undefined) const SetOnlineStatusMessageContext = React.createContext(() => undefined) @@ -13,8 +14,7 @@ export const OnlineStatusMessageProvider = ({ }: { children: ReactNode }): ReactElement => { - const [onlineStatusMessage, setOnlineStatusMessage] = - useState(null) // note: not undefined + const [onlineStatusMessage, setOnlineStatusMessage] = useState() // note: not undefined return ( @@ -28,28 +28,11 @@ export const OnlineStatusMessageProvider = ({ } export const useOnlineStatusMessageValue = () => { - const onlineStatusMessage = useContext(OnlineStatusMessageValueContext) - - // note: value is initialized to `null` in provider, not undefined - if (onlineStatusMessage === undefined) { - throw new Error( - 'useOnlineStatusMessageValue must be used within an OnlineStatusMessageProvider' - ) - } - - return onlineStatusMessage + return useContext(OnlineStatusMessageValueContext) } export const useSetOnlineStatusMessage = () => { - const setOnlineStatusMessage = useContext(SetOnlineStatusMessageContext) - - if (setOnlineStatusMessage === undefined) { - throw new Error( - 'useSetOnlineStatusMessage must be used within an OnlineStatusMessageProvider' - ) - } - - return setOnlineStatusMessage + return useContext(SetOnlineStatusMessageContext) } // combination of both getter and setter (also provides backward compatability)