Objective: When my Game component receives a prop I want componentDidUpdate() to call a function called consolelog which is a child of another function called gamecode().
Problem: using this.gameCode.consolelog() always returns an error saying that consolelog is not a function of gameCode.
Note: Gamecode() is imported into my react component (Game) from a file called gamecode.js
Component:
import React, {Component} from 'react';
import {gameCode, showLeaderboard} from '../utility/gamecode';
class Game extends Component{
constructor(props){
super();
this.state = {
drawL : null
}
};
componentDidMount(){
this.gameCode();
};
componentDidUpdate(){
//will run when the state changes
if(this.props.leaderboard){
var a = this.props.leaderboard;
this.gameCode.consolelog(a);
}
}
render(){
return(
<div className="Game">
<canvas id="canvas"></canvas>
<button onClick={this.props.getleaderboard} className="showleaders">Show leaderboard</button>
</div>
);
}
}
export default Game;
GameCode.js function:
gameCode(){
//the main function initializes some stuff immediately and start running the update() and draw() functions
const canvas = document.getElementById('canvas');
canvas.width = 800;
canvas.height= 600;
const ctx = canvas.getContext("2d");
const FPS = 30;
setInterval(() => {
update();
draw();
}, 1000/FPS);
//but sometimes I need to call other functions on demand, but these functions depend on what the parent function and other sibling functions
function consolelog(a){
console.log("success!");
console.log(a);
}
}
consolelogas its own function, and import it into your module?GameCode(). This is a canvas html game and so I have subfunctions calledupdateanddrawthat draw to the canvas and I don't want multiple functions drawing to it.