Skip to content
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

Don't complain about "initAsync called twice" when hot reloading #114

Closed
4 tasks
statico opened this issue Oct 3, 2023 · 5 comments
Closed
4 tasks

Don't complain about "initAsync called twice" when hot reloading #114

statico opened this issue Oct 3, 2023 · 5 comments
Labels
enhancement New feature or request react-native

Comments

@statico
Copy link

statico commented Oct 3, 2023

Is your feature request related to a problem?

Similar to #113 — every time my React Native app is loaded, reloaded, or the base layout changes (we're using expo-router), PostHog throws up a giant warning:

PostHog.initAsync called twice with the same apiKey. The first instance will be used.

It would be nice to disable this message when __DEV__ is true.

Recoil, a state management library for React, had a similar behavior with singletons and hot reloading and solved this by allowing developers to add a RECOIL_DUPLICATE_ATOM_KEY_CHECKING_ENABLED=false setting: facebookexperimental/Recoil#733

Describe the solution you'd like

Some kind of setting, maybe in PostHogProvider, like dangerouslyIgnoreReinitialization or iKnowWhatImDoing={__DEV__} or something, that removes this warning.

Describe alternatives you've considered

  • Patching the package with patch-package

Related sub-libraries

  • All of them
  • posthog-web
  • posthog-node
  • posthog-react-native

Additional context

<3

@statico statico added the enhancement New feature or request label Oct 3, 2023
@hikchoi
Copy link

hikchoi commented Nov 21, 2023

seconding this. pretty harmless, but it makes it hard to read other logs that I actually want to read :(

@statico are you still patching the package on your end or have you found any other workarounds?

@statico
Copy link
Author

statico commented Nov 21, 2023

@hikchoi Yep, still patching this and a few other messages. Previously with patch-package, now with pnpm's built-in patcher.

@marandaneto
Copy link
Member

Maybe the solution here is to patch the Router and not call PostHog.initAsync again when a hot reload just happened, you can do that via env. var. (similar to 113) or a global variable that survives hot reload.

We already do that here but if the whole Provider gets recreated, this field is also gone, the solution would be to depend on an external global variable.

@edumana
Copy link

edumana commented Jan 15, 2024

I was getting this error and solved it by putting PH provider at the very top of the RootLayout, and moving everything to a new App child component. I had some auth states changing which were causing PH provider to reload. So my app now looks like this:

const RootLayout = () => {
  return (
      <PostHogProvider
        apiKey={process.env.EXPO_PUBLIC_POSTHOG_API_KEY}
        options={{ host: process.env.EXPO_PUBLIC_POSTHOG_HOST}}
      >
        <App /> // <------ all child components with changing states 
      </PostHogProvider>
  );
};

export default RootLayout;

@marandaneto
Copy link
Member

Closed by #179
The warning is removed since it's not useful anyway, you can still do this to spare the recreation bits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request react-native
Projects
None yet
Development

No branches or pull requests

4 participants