Skip to content

Instantly share code, notes, and snippets.

@devesh2605
Created November 21, 2019 06:54
Show Gist options
  • Save devesh2605/bc4caa10b2a0e782a6ab76733ecfa24a to your computer and use it in GitHub Desktop.
Save devesh2605/bc4caa10b2a0e782a6ab76733ecfa24a to your computer and use it in GitHub Desktop.
import React from 'react';
class OrientationDetection extends React.Component {
constructor(props) {
super(props);
this.state = {
orientation: 0
}
}
componentDidMount() {
window.addEventListener('orientationchange', () => {
this.setState({
orientation: window.orientation
}, () => {
console.log('orientation: ', this.state.orientation);
})
}, false);
}
componentWillUnmount() {
window.removeEventListener('orientationchange', () => {
console.log('removed orientationchange');
}, false);
}
render() {
return (
<div>
{this.state.orientation === 90 ? <p>I am only Visible in Landscape</p> : null}
</div>
)
}
}
export default OrientationDetection;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment