OneSignal / react-onesignal

React OneSignal Module: Make it easy to integrate OneSignal with your React App!
Other
73 stars 26 forks source link

[Bug]: The OneSignal web SDK can only be initialized once. Extra initializations are ignored. Please remove calls initializing the SDK more than once. #129

Closed SneakingSlayer closed 11 months ago

SneakingSlayer commented 11 months ago

What happened?

The package seems to be throwing this new error:

OneSignal: The OneSignal web SDK can only be initialized once. Extra initializations are ignored. Please remove calls initializing the SDK more than once.

Regardless of how you initialize OneSignal.

Code below:

const [isInitializedOneSignal, setIsInitializedOneSignal] = useState(false);

 const initOneSignal = useCallback(async () => {
     // If OneSignal is already initialized then return
     if (isInitializedOneSignal) {
      return;
    }

    // Initialize OneSignal
    await OneSignal.init({ 
      appId: 'MY-APP-ID', 
      allowLocalhostAsSecureOrigin: true
    });

    setIsInitializedOneSignal(true);

    // Open subscribe prompt
    OneSignal.Slidedown.promptPush();
  }, [isInitializedOneSignal]);
  // Initialize One Signal
  useEffect(() => {
    void initOneSignal();
  },[initOneSignal]);

What browsers are you seeing the problem on?

Firefox, Chrome (Chromium), Safari, Microsoft Edge, Opera, Brave

What operating system are you running?

asdasasdasasdasasdasasdas

Steps to reproduce?

1. Initialize OneSignal
2. Run the app

What did you expect to happen?

Should not throw any errors

Relevant log output

No response

SneakingSlayer commented 11 months ago

Just realized React's StrictMode renders everything twice hence this error.