Can't remove device motion listener

#1

Hi - I can successfully add a device motion listener:

      const motionListener = DangerZone.DeviceMotion.addListener(this.onDeviceMotion);
      this.setState({
        motionListener: motionListener,
      });
      DangerZone.DeviceMotion.setUpdateInterval(this.props.motionInterval)

But I can’t remove the listener. I’ve tried:

this.state.motionListener.remove();

which returns that it does not include the function ‘remove’

as well as

DangerZone.DeviceMotion. removeAllListeners()

which gives an error something like “attempted to remove more listeners than exist”

#2

Hey @heaversm,

Don’t put your listener into the component’s state. Use this.motionListener = DangerZone... (usually in componentDidMount) and then call this.motionListener.remove() (usually in componentWillUnmount) or whenever you want to remove the listener. Only use removeAllListeners() if you are creating and using multiple listeners.

Cheers,

Adam

#3

Thanks, that worked!

1 Like
#4

This worked, thank you!

closed #5

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.