Modern smartphones have physical features like notches (for cameras), dynamic islands, and home indicators (the bar at the bottom) that overlay the screen.
React Native provides a built-in <SafeAreaView> component.
SafeAreaView can feel "janky" or choppy during page transitions, especially when using complex navigation libraries like Expo Router.useSafeAreaInsetsFor smoother performance and more control, we can use the useSafeAreaInsets hook from the react-native-safe-area-context library. This hook gives us the exact pixel values for the "unsafe" areas on all four sides.
{ top, bottom, left, right }.View.To make our development easier, we can update our reusable ThemedView component to handle safe areas automatically based on a prop.
We don’t always need the view to be safe right? sometime, some sections on the page that doesn’t require small extra padding around it. So we need this to be a Choice.
File: ./components/ThemedView.jsx
import { View, useColorScheme } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { Colors } from "../constants/Colors";
const ThemedView = ({ style, safe = false, ...props }) => {
const colorScheme = useColorScheme();
const theme = Colors[colorScheme] ?? Colors.light;
// If SAFE Prop -> FALSE, return a regular themed view
if (!safe) {
return (
<View style={[{ backgroundColor: theme.background }, style]} {...props} />
);
}
const ***insets*** = useSafeAreaInsets() // {"bottom": 0, "left": 0, "right": 0, "top": 43.13725662231445}
// If SAFE Prop -> TRUE, apply manual padding based on device insets
return (
<View
style={[{
backgroundColor: theme.background,
paddingTop: ***insets***.top,
paddingBottom: ***insets***.bottom,
},
style,
]}
{...props}
/>
);
};
export default ThemedView;