Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!