Can't remove device motion listener

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”

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

Thanks, that worked!

1 Like

This worked, thank you!

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