Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda