Skip to content

Instantly share code, notes, and snippets.

@tejas77
Created June 15, 2020 17:30
Show Gist options
  • Save tejas77/7b23348c956a1144ae9cb118dee4c9b9 to your computer and use it in GitHub Desktop.
Save tejas77/7b23348c956a1144ae9cb118dee4c9b9 to your computer and use it in GitHub Desktop.
Adding the Device Motion Listener
const [data, setData] = useState({});
//Call Once when Screen loads
useEffect(() => {
//Subscribe Function
_subscribe();
//Call Once when Screen unloads
return () => {
_unsubscribe(); //Unsubscribe Function
};
}, []);
//SetInterval between listening of 2 DeviceMotion Action
const _setInterval = () => {
DeviceMotion.setUpdateInterval(77);
};
const _subscribe = () => {
//Adding the Listener
DeviceMotion.addListener((devicemotionData) => {
setData(devicemotionData.rotation);
});
//Calling setInterval Function after adding the listener
_setInterval();
};
const _unsubscribe = () => {
//Removing all the listeners at end of screen unload
DeviceMotion.removeAllListeners();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment