Reputation: 95
Im learning how to use device orientation sensor in React Native app through https://www.w3.org/TR/orientation-event/ and https://developer.mozilla.org/en-US/docs/Web/API/Detecting_device_orientation.
My App.js:
const App = () => {
window.addEventListener("deviceorientation", onOrientationChanged,true);
function onOrientationChanged(event) {
var absolute = event.absolute;
var alpha = event.alpha;
var beta = event.beta;
var gamma = event.gamma;
console.log(beta);
}
return (
<>
<ScrollView
contentInsetAdjustmentBehavior="automatic">
<Text>kfkasdfasd</Text>
</ScrollView>
</>
);
};
Running this results in:
TypeError: window.addEventListener is not a function. (In 'window.addEventListener("deviceorientation", onOrientationChanged, true)', 'window.addEventListener' is undefined)
I couldnt google anyone having the same problem, so I am confused as how to solve this when I followed documentation and did not add anything extra.
Upvotes: 2
Views: 1064
Reputation: 1715
The window object does not exist in React Native.
Check out the useDimension hook from react-native-hooks https://github.com/react-native-community/hooks#usedimensions
or if you want more details checkout https://github.com/react-native-sensors/react-native-sensors
Upvotes: 2