A react-native library for obtaining current device orientation
npm install --save react-native-orientation-listener
Libraries
folder and select "Add files to [your project]"RCTOrientationListener.xcodeproj
from your node_modules
folderBuild Phases
tab.+
button underneath Link Binary With Libraries
section.libRCTOrientationListener.a
/android/settings.gradle
include ':app'
with:include ':com.walmartreact.ReactOrientationListener', ':app'
project(':com.walmartreact.ReactOrientationListener').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-orientation-listener/android')
android/app/build.gradle
dependencies
:compile project(':com.walmartreact.ReactOrientationListener')
MainActivity.java
file under android/src
import com.walmartreact.ReactOrientationListener.*;
.addPackage(new MainReactPackage())
:.addPackage(new ReactOrientationListener())
Import the library:
var Orientation = require('react-native-orientation-listener');
This method will return the current orientation and device string in the form of a callback:
componentDidMount(){
Orientation.getOrientation(
(orientation, device) => {
console.log(orientation, device);
}
);
}
This method will add a listener that will call the callback anytime the device orienatation changes:
_setOrientation(data) {
this.setState({
orientation: evt.orientation,
device: evt.device
});
},
componentDidMount(){
Orientation.addListener(this._setOrientation);
}
This method removes the listener you added in componentDidMount:
componentWillUnmount() {
Orientation.removeListener(this._setOrientation);
}