Skip to content

Instantly share code, notes, and snippets.

@abakers
Created September 8, 2022 09:00
Show Gist options
  • Save abakers/0dcb56abc082a26c89da52a8841daae6 to your computer and use it in GitHub Desktop.
Save abakers/0dcb56abc082a26c89da52a8841daae6 to your computer and use it in GitHub Desktop.
useGetStatusBarHeight
export const useGetStatusBarHeight = (): number => {
const [statusBarHeight, setStatusBarHeight] = useState(0);
const isMounted = useRef(true);
useEffect(() => {
if (Platform.OS === 'android') {
isMounted.current &&
setStatusBarHeight(
NativeModules.StatusBarManager.HEIGHT,
);
} else {
NativeModules.StatusBarManager.getHeight(
(status: { height: number }) => {
isMounted.current &&
setStatusBarHeight(status.height);
},
);
}
return () => {
isMounted.current = false;
};
}, []);
return statusBarHeight;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment