I am using portrait mode in react-native application. But I want to capture the rotation event of the screen. Is there a way to do this?
Thanks...
For Android, open the AndroidManifest. xml file and within the activity element add 'android:screenOrientation="portrait"' to lock to portrait or 'android:screenOrientation="landscape"' to lock to landscape.
on iOS you need to allow your app to rotate. In XCode you need to go to target and then in the Deployment Info -> Device Orientation enable the orientaitons your app should support. For android you'll have to modify your AndroidManifest file and set the screenOrientation on the activity tag.
this function might help you
create a useOrientation.js
file
import {useEffect, useState} from 'react';
import {Dimensions} from 'react-native';
export function useOrientation(){
const [orientation, setOrientation] = useState("PORTRAIT");
useEffect(() => {
Dimensions.addEventListener('change', ({window:{width,height}})=>{
if (width<height) {
setOrientation("PORTRAIT")
} else {
setOrientation("LANDSCAPE")
}
})
}, []);
return orientation;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With