Try like this
class HomePage extends React.Component {
runBubbles = () => {
// instantiate some css bubbles on screen
}
clearAndRunBubbles = () => {
// clear bubbles from DOM
this.runBubbles()
}
componentDidMount(){
this.runBubbles()
// on resize, re-run bubbles code with new screen dimensions:
window.addEventListener("resize", this.clearAndRunBubbles);
}
}
For arrow function you don't need to have the this binding because by default it takes outer lexical scope.
or
class HomePage extends React.Component {
constructor() {
this.runBubbles = this.runBubbles.bind(this);
this.clearAndRunBubbles = this. clearAndRunBubbles.bind(this);
}
runBubbles(){
// instantiate some css bubbles on screen
}
clearAndRunBubbles(){
// clear bubbles from DOM
this.runBubbles()
}
componentDidMount(){
this.runBubbles()
// on resize, re-run bubbles code with new screen dimensions:
window.addEventListener("resize",this.clearAndRunBubbles);
}
}
Whenever calling a standard function inside a class component in react you have to add this binding to make sure the function is binds to current class component.
this.runBubbles()componentDidMount, changerunBubbles()tothis.runBubbles()