Skip to main content

useOrientation

The useOrientation hook reports the device screen orientation and updates whenever it changes. It uses the Screen Orientation API and falls back to comparing the window width and height in browsers that do not support it.

Usage​

Installation​

import { useOrientation } from "hookstorm";

Parameters​

This hook does not take any parameters.

Return Values​

  • angle — The rotation angle in degrees (0, 90, 180 or 270). Always 0 in the fallback mode.
  • type — The orientation type, for example portrait-primary or landscape-secondary.

Example Usage​

import { ReactElement } from "react";
import { useOrientation } from "hookstorm";

export default function OrientationInfo(): ReactElement {
const { type, angle } = useOrientation();

return (
<p>
{type.startsWith("landscape") ? "Landscape" : "Portrait"} ({angle}°)
</p>
);
}

Notes​

  • During server-side rendering the hook reports portrait-primary with an angle of 0, then updates on the client.
  • To only react to the orientation, useMediaQuery with (orientation: landscape) is a lighter alternative.