From 96887b71d93f305e4b5bdc8d85d460cd4aff5b83 Mon Sep 17 00:00:00 2001 From: Dan Rittman Date: Wed, 24 May 2023 08:38:20 -0400 Subject: [PATCH] Change event handlers from anonymous functions so they can be properly removed --- src/Joystick.tsx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Joystick.tsx b/src/Joystick.tsx index 06b1efe..1e254ae 100644 --- a/src/Joystick.tsx +++ b/src/Joystick.tsx @@ -185,8 +185,8 @@ class Joystick extends React.Component { dragging: true }); - window.addEventListener(InteractionEvents.PointerUp, event => this._pointerUp(event)); - window.addEventListener(InteractionEvents.PointerMove, event => this._pointerMove(event)); + window.addEventListener(InteractionEvents.PointerUp, this._pointerUp); + window.addEventListener(InteractionEvents.PointerMove, this._pointerMove); this._pointerId = e.pointerId //@ts-ignore this._stickRef.current.setPointerCapture(e.pointerId); @@ -244,7 +244,7 @@ class Joystick extends React.Component { * @param event * @private */ - private _pointerMove(event: PointerEvent) { + private _pointerMove = (event: PointerEvent) => { event.preventDefault() if (this.state.dragging) { if(!this.props.followCursor && event.pointerId !== this._pointerId) return; @@ -286,7 +286,7 @@ class Joystick extends React.Component { * Handle pointer up and de-register listen events * @private */ - private _pointerUp(event: PointerEvent) { + private _pointerUp = (event: PointerEvent) => { if(event.pointerId !== this._pointerId) return; const stateUpdate = { dragging: false, @@ -300,8 +300,8 @@ class Joystick extends React.Component { } }); - window.removeEventListener(InteractionEvents.PointerUp, event => this._pointerUp(event)); - window.removeEventListener(InteractionEvents.PointerMove, event => this._pointerMove(event)); + window.removeEventListener(InteractionEvents.PointerUp, this._pointerUp)); + window.removeEventListener(InteractionEvents.PointerMove, this._pointerMove); this._pointerId = null; if (this.props.stop) { this.props.stop({