Skip to content

bug: Destructuring posthog variable causes it to not work #1915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jay-babu opened this issue Apr 23, 2025 · 2 comments
Open

bug: Destructuring posthog variable causes it to not work #1915

jay-babu opened this issue Apr 23, 2025 · 2 comments
Assignees

Comments

@jay-babu
Copy link

jay-babu commented Apr 23, 2025

using posthog react

the first works fine:

  const posthog = usePostHog();

  useEffect(() => {
	posthog.capture(...)
  },[posthog]);

the second does not work

  const { capture } = usePostHog();

  useEffect(() => {
	capture(...)
  },[capture]);
@jay-babu jay-babu changed the title Destructuring posthog variable causes it to not work bug: Destructuring posthog variable causes it to not work Apr 23, 2025
@jay-babu
Copy link
Author

error message is:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '__loaded')

@rafaeelaudibert
Copy link
Member

rafaeelaudibert commented Apr 23, 2025

This is happening because we're returning an instance of a class that depends on their this value, not really a raw object. this does not bind how you'd expect on classes, so this is expected to fail.

You could bind the right this by calling capture.bind(posthog)() but in that case you might as well just call posthog.capture()

This is our intended usage, so Im tending towards triaging this as not a bug

@rafaeelaudibert rafaeelaudibert self-assigned this Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants