1

I'm trying to run this only once on load, however, due to React.StrictMode I'm being told it's running twice. I've tried a few recommended solutions however none have worked. what do I need to do so it only runs once?

const query = useCallback(async () => {
    setLoading(true);
    try {
      console.log("Starting game in async")
      window.Phaser = await import('phaser')
      const Boot = await import('./bankPanicScenes/Boot.js')
      const Preloader = await import( './bankPanicScenes/Preloader.js')
      const MainMenu = await import( './bankPanicScenes/MainMenu.js')
      const MainGame = await import( './bankPanicScenes/Game.js')

      const config = {
        type: Phaser.AUTO,
        gameTitle: "Bank Panic",
        parent: "game-bankpanic",
        autoCenter: Phaser.Scale.CENTER_HORIZONTALLY,
        autoFocus: true,
        fps: {
          target: 60,
        },
        physics: {
          default: "arcade",
          arcade: {
            gravity: { y: 200 },
            debug: false,
          },
        },
        backgroundColor: "#282c34",
        scale: {
          mode: Phaser.Scale.ScaleModes.NONE,
        },
        scene: [Boot, Preloader, MainMenu, MainGame],
      }
      game.current = new Phaser.Game(config)
      console.log("Game loaded")
    } catch (e) {
      console.log(e)
    } finally {
      setLoading(false);
      console.log('Loading Finished')
      
    }
  }, [])

  useEffect(() => {
    query()
  }, [])
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
Noah B
  • 47
  • 4
  • Did you try removing strict mode? – Phil Jul 07 '22 at 04:06
  • Does this answer your question? [My React Component is rendering twice because of Strict Mode](https://stackoverflow.com/questions/61254372/my-react-component-is-rendering-twice-because-of-strict-mode) – Phil Jul 07 '22 at 04:07

0 Answers0