Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

116
Vistas
Phaser scene duplicated when integrating with React

when embedding this single scene phaser game into React page, the scene was duplicated into two. And every time updating the code, 2 more duplications are added on the page.

Game componenet as below:

        import MainScene from './scenes/MainScene.js';
        import Phaser from 'phaser';
        import React, { Component } from 'react'

        class Game extends Component {

        componentDidMount(){
          const config = {
              width: 640,
              height: 1024,
              backgroundColor: '#333333',
              type: Phaser.AUTO,
              parent: 'phaser-game',
              scene: [MainScene]
            };      
          new Phaser.Game(config);
        }
        shouldComponentUpdate() {
          return false;
        }
        render() {
        return <div id="game" />
        }
    
        }

        export default Game;

here can see two canvas elements are created enter image description here

thanks in advance!

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

I assume the problem lies in the config object, where you are setting the parent to phaser-game. parentis the idof the DOM-element, where the canvas should be injected. If an element with that specific id can't be found, it is injected into the body-tag.

So the solution is, to change the parent property from phaser-game to game, as this seems to be the id of the element, where you want the game to be displayed.

Just to show the connection, between the parent property and the element it refers to.

Here some code:

 // ...
 componentDidMount(){
     const config = {
         // ... 
         // "id" of the parent DOM Element
         parent: 'game',
         // ...
     };      
     new Phaser.Game(config);
 }
 // ...
 render() {
    // parent DOM Element
    return <div id="game" />
 }

Update:
the problem could be that, componentDidMount can run multiple times as mentioned in this article, since I don't know your code and I'm no ReactJs expert, you could try to look into this article, how to prevent multiple call of the componentDidMount function.

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda