Home How to get statusbar height in React Native
Post
Cancel

How to get statusbar height in React Native

There are different ways of getting the statusBarHeight in React Native and I will show some ways.

Expo

If you’re using Expo you can use Constants.statusBarHeight.

1
2
import Constants from 'expo-constants';
const statusBarHeight = Constants.statusBarHeight;

Vanilla React Native (with React Navigation)

If you’re using Vanilla React Native with React Navigation you can use the following:

1
2
3
4
5
6
import { useSafeAreaInsets } from 'react-native-safe-area-context';

export default function ComponentName() {
  const insets = useSafeAreaInsets();
  const statusBarHeight = insets.top;
}

See: https://reactnavigation.org/docs/handling-safe-area/#use-the-hook-for-more-control

Sample Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import * as React from 'react';
import { Text, View, StatusBar, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import { useSafeAreaInsets, SafeAreaProvider } from 'react-native-safe-area-context';

export default function App() {
  return (
    <SafeAreaProvider>
      <ChildScreen />
    </SafeAreaProvider>
  );
}

function ChildScreen() {
  const insets = useSafeAreaInsets();
  
  return (
    <View style={styles.container}>
      <Text>
        {insets.top}
      </Text>
      <Text>
        {Constants.statusBarHeight}
      </Text>
      <Text>
        {StatusBar.currentHeight ?? 'N/A'}
      </Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1, 
    justifyContent: 'center',
  },
});

Output:

 Samsung Galaxy S10 5GiPhone 8 PlusiPhone 11 Pro MaxWeb
insets.top39.7142868041992220440
Constants.statusBarHeight3920440
StatusBar.currentHeight ?? 'N/A'39.42856979370117N/AN/AN/A

Live Demo: https://snack.expo.dev/@dcangulo/statusbarheight

This post is licensed under CC BY 4.0 by the author.

How to convert char to int in C/C++

Check if Redis is running