Rooks

useOrientation

About

Hook to detect and track device orientation changes in React applications.


Examples

import React from "react";
import { useOrientation } from "rooks";
 
export default function App() {
  const orientation = useOrientation();
 
  if (!orientation) {
    return <div>Orientation API not supported</div>;
  }
 
  return (
    <div>
      <h1>Device Orientation</h1>
      <div>
        <strong>Angle:</strong> {orientation.angle}°
      </div>
      <div>
        <strong>Type:</strong> {orientation.type}
      </div>
      
      <div style={{ marginTop: '20px' }}>
        <p>Try rotating your device to see the orientation change!</p>
        
        {orientation.type.includes('portrait') && (
          <div style={{ color: 'blue' }}>
            📱 Portrait mode detected
          </div>
        )}
        
        {orientation.type.includes('landscape') && (
          <div style={{ color: 'green' }}>
            🔄 Landscape mode detected
          </div>
        )}
      </div>
    </div>
  );
}

Arguments

This hook takes no arguments.

Return value

Return valueTypeDescription
orientationScreenOrientation | nullCurrent orientation object with angle and type properties, or null if not supported

ScreenOrientation Properties

PropertyTypeDescription
anglenumberThe orientation angle in degrees (0, 90, 180, or 270)
typestringThe orientation type (e.g., "portrait-primary", "landscape-primary", "portrait-secondary", etc.)

On this page