Created
June 15, 2020 17:30
-
-
Save tejas77/7b23348c956a1144ae9cb118dee4c9b9 to your computer and use it in GitHub Desktop.
Adding the Device Motion Listener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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