I want to start React Native app with Portrait orientation. Currently I’m using the react-native-orientation-locker library to handle the initial orientation. But it’s not working correctly.
My App.js looks like this.
import Orientation from 'react-native-orientation-locker';
export const App = () => {
useLayoutEffect(() => {
Orientation.lockToPortrait();
}, []);
return (
<RootContainer>
<MyApp />
</RootContainer>
);
};
export default App;
I think this has to be handled from the native side because the JavaScript codes run after the initial process. Maybe I have to add a code MainApplication.java and AppDelegate.mm?
3
Answers
You should wait until you lock the orientation. See the example below:
There is a possibility that the useLayoutEffect hook is causing issues.
If you use useLayoutEffect to lock orientation, your screen may flicker or render slowly because it is called after all DOM mutations are complete.
In most cases, it’s better to use the useEffect hook because it’s called asynchronously after rendering is complete.
If you want the application to have a static orientation and not allow it to change dynamically inside of the app, you should add the orientation natively for both Android and iOS.
For Android:
Open up
AndroidManifest.xml
and add the following inside of the application tagandroid:screenOrientation="portrait
So you’d have something like this
For iOS:
Open up xCode and inside of the general tab, go down to Deployment info and check the Portrait box