UNPKG

react-native-orientation-bits

Version:

A react-native module that can listen on orientation changing of device, get current orientation, lock to preferred orientation.

23 lines (19 loc) 576 B
import { useRef, useEffect } from 'react'; import Orientation from '../orientation' export function useDeviceOrientationChange(callback) { const savedCallback = useRef(); useEffect(() => { savedCallback.current = callback; }, [callback]); useEffect(() => { function listener(ori) { savedCallback.current(ori); } const initial = Orientation.getInitialOrientation(); listener(initial); Orientation.addDeviceOrientationListener(listener); return () => { Orientation.removeDeviceOrientationListener(listener); }; }, []); }