From b1062af145095f1957a7bd0f306d714afb5b5f80 Mon Sep 17 00:00:00 2001 From: goenning Date: Fri, 1 Sep 2023 17:57:07 +0100 Subject: [PATCH] don't throw error --- .vscode/settings.json | 1 + packages/react/src/index.tsx | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 7aa6142..ac0d179 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "editor.tabSize": 2, "editor.formatOnSave": true, + "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.tabCompletion": "on", "[typescript]": { "editor.codeActionsOnSave": { diff --git a/packages/react/src/index.tsx b/packages/react/src/index.tsx index a742cdf..6214ea4 100644 --- a/packages/react/src/index.tsx +++ b/packages/react/src/index.tsx @@ -1,7 +1,7 @@ -'use client'; +"use client"; -import { init, trackEvent, type AptabaseOptions } from '@aptabase/web'; -import { createContext, useContext, useEffect } from 'react'; +import { init, trackEvent, type AptabaseOptions } from "@aptabase/web"; +import { createContext, useContext, useEffect } from "react"; globalThis.__APTABASE_SDK_VERSION__ = `aptabase-react@${process.env.PKG_VERSION}`; @@ -27,15 +27,20 @@ export function AptabaseProvider({ appKey, options, children }: Props) { init(appKey, options); }, [appKey, options]); - return {children}; + return ( + + {children} + + ); } export function useAptabase(): AptabaseClient { const ctx = useContext(AptabaseContext); - if (!ctx) { - throw new Error( - 'useAptabase must be used within AptabaseProvider. Did you forget to wrap your app in ?', + if (!ctx.appKey) { + console.error( + "Aptabase: useAptabase must be used within AptabaseProvider. Did you forget to wrap your app in ?" ); + return { trackEvent: () => Promise.resolve() }; } return { trackEvent };