PostHog / posthog-js-lite

Reimplementation of posthog-js to be as light and modular as possible.
https://posthog.com/docs/libraries
MIT License
63 stars 31 forks source link

undefined is not a function #242

Closed devashray-jumbo closed 2 months ago

devashray-jumbo commented 3 months ago

Bug description

I have integrated posthog in my expo app successfully . Events are getting recored in dashboard too but when i am trying to use useFeatureFlag hook it throws an error saying undefined is not a function . Error is located in useFeatureFlag,js . Digging more into the error it says getFeatureFlag is not a function . Now this is defined in node_modules folder

Please describe.

How to reproduce

  1. Simply try using useFeatureFlag hook in your component
  2. Pass the feature flag in the hook 3.The app will crash

Related sub-libraries

Additional context

Thank you for your bug report – we love squashing them!

marandaneto commented 3 months ago

@devashray-jumbo Hi, which version of the SDK are you using? Which version of RN and Expo itself? Can you provide a few code snippets of the PostHog SDK setup? so I can check things on my end.

Thanks

devashray-jumbo commented 3 months ago

Hi Please find the details below

posthog-react-native - 3.1.1 react-native - 0.71.14 expo - 48.0.21 export const projectApiKey = 'XXX'; export const projectServerUrl = 'https://us.i.posthog.com'; export const posthog = new PostHog(projectApiKey, { host: projectServerUrl, disabled: false, defaultOptIn: true, sendFeatureFlagEvent: true, preloadFeatureFlags: true, disableGeoip: false, captureNativeAppLifecycleEvents: true, }); export const logPosthogEvent = ({ eventName, eventAttribute = {}, }: { eventName?: string; eventAttribute?: any; }) => { if (eventAttribute) { posthog.capture(eventName, { ...eventAttribute }); return; } posthog.capture(eventName); };

On Mon, Jul 1, 2024 at 11:13 AM Manoel Aranda Neto @.***> wrote:

@devashray-jumbo https://github.com/devashray-jumbo Hi, which version of the SDK are you using? Which version of RN and Expo itself? Can you provide a few code snippets of the PostHog SDK setup? so I can check things on my end.

Thanks

— Reply to this email directly, view it on GitHub https://github.com/PostHog/posthog-js-lite/issues/242#issuecomment-2199283762, or unsubscribe https://github.com/notifications/unsubscribe-auth/BEXLYGFSV6SHD47FTY5FOVLZKDUARAVCNFSM6AAAAABKC7EQU2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJZGI4DGNZWGI . You are receiving this because you were mentioned.Message ID: @.***>

devashray-jumbo commented 3 months ago

When using feature flag hook the app crashes throwing error "undefined is not a function"

import { useFeatureFlag } from 'posthog-react-native' const testFlag = useFeatureFlag('Test')

On Mon, Jul 1, 2024 at 1:10 PM Devashray . @.***> wrote:

Hi Please find the details below

posthog-react-native - 3.1.1 react-native - 0.71.14 expo - 48.0.21 export const projectApiKey = 'phc_cWZmmd0TLwconAucvMlzvv7Byim5iZ3ngILJmYm9VC1'; export const projectServerUrl = 'https://us.i.posthog.com'; export const posthog = new PostHog(projectApiKey, { host: projectServerUrl, disabled: false, defaultOptIn: true, sendFeatureFlagEvent: true, preloadFeatureFlags: true, disableGeoip: false, captureNativeAppLifecycleEvents: true, }); export const logPosthogEvent = ({ eventName, eventAttribute = {}, }: { eventName?: string; eventAttribute?: any; }) => { if (eventAttribute) { posthog.capture(eventName, { ...eventAttribute }); return; } posthog.capture(eventName); };

On Mon, Jul 1, 2024 at 11:13 AM Manoel Aranda Neto < @.***> wrote:

@devashray-jumbo https://github.com/devashray-jumbo Hi, which version of the SDK are you using? Which version of RN and Expo itself? Can you provide a few code snippets of the PostHog SDK setup? so I can check things on my end.

Thanks

— Reply to this email directly, view it on GitHub https://github.com/PostHog/posthog-js-lite/issues/242#issuecomment-2199283762, or unsubscribe https://github.com/notifications/unsubscribe-auth/BEXLYGFSV6SHD47FTY5FOVLZKDUARAVCNFSM6AAAAABKC7EQU2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJZGI4DGNZWGI . You are receiving this because you were mentioned.Message ID: @.***>

marandaneto commented 3 months ago

@devashray-jumbo If you wanna use the usePostHog, useFeatureFlag, and other hooks, you need to set up PostHog with the PostHogProvider otherwise the hooks won't know about yourposthog instance, or at least do this.

export function MyApp() {
  return <PostHogProvider client={posthog}>{/* Your app code */}</PostHogProvider>
}

If you don't wanna use the PostHogProvider, then for instance of using the hooks, just use the posthog.getFeatureFlag(...) and similar methods.

devashray-jumbo commented 3 months ago

Hi Manoel Aranda Neto , I tried using the Provider as mentioned , but as soon as I used usePostHog hook , the app crashes saying undefined is not a function.

Digging into the issue , I found out that the getAppProperties function is causing the issue. If I go into the posthog-react-native package in node_modules and remove this method getAppProperties , the SDK works fine in that case.

FYI , i have installed all the other dependency which were mentioned in the docs [image: image.png]

Please find the code snippet below

In Login Component I am using the usePostHog hook const posthog = usePostHog(); On Mon, Jul 1, 2024 at 1:20 PM Manoel Aranda Neto ***@***.***> wrote: > @devashray-jumbo If you wanna use > the usePostHog, useFeatureFlag, and other hooks, you need to set up > PostHog with the PostHogProvider > > otherwise the hooks won't know about yourposthog instance, or at least do > this > > . > > export function MyApp() { > return {/* Your app code */} > } > > If you don't wanna use the PostHogProvider, then for instance of using > the hooks, just use the posthog.getFeatureFlag(...) and similar methods. > > — > Reply to this email directly, view it on GitHub > , > or unsubscribe > > . > You are receiving this because you were mentioned.Message ID: > ***@***.***> >
marandaneto commented 3 months ago

@devashray-jumbo hey, getAppProperties cannot return null or undefined so it's very strange.

We are using this in our samples and I just tested it out and everything is working fine.

The code snippets are incomplete so I cannot spot the issue besides that you are wrapping PostHogProvider with other things I don't know such as Provider, APIProvider, etc, you are probably trying to use usePostHog even before the PostHogProvider is created.

PostHogProvider must be ideally the root so it's created before anything.

Can you provide a minimal reproducible example? Just create a sample app from scratch and try to add PostHog, so you can demonstrate the bug or find out that there's something off in your implementation.

Thanks.

Best, Manoe,

marandaneto commented 2 months ago

Closing the issue as a part of large repository cleanup, due to it being inactive and/or outdated. Please do not hesitate to ping me if it is still relevant, and I will happily reopen and work on it. Cheers!