A component called SafeAreaView
is exported by react-native, react-navigation, react-native-safe-area-context and react-native-safe-area-view.
What are the differences and which one should I use in which cases?
Except for the one in react-native
they build on top of one another. All the others instruct that you need to wrap your whole app inside a SafeAreaProvider
component.
I dug into the source code a bit and this is my deductions:
The default implementation provided with React Native. Should work for most cases but doesn't e.g. provide inset amounts programmatically.
Provides detailed, retrievable inset information and a rather bare-bones implementation of SafeAreaView
.
Written on top of react-native-safe-area-context
, it re-exports SafeAreaProvider
and various other methods, but provides a more complex/fancy implementation of SafeAreaView
that uses Animated.View
. Adds properties such as forceInset
to avoid jankiness in some cases due to layout updates. Implemented by the React Navigation team.
Re-exports SafeAreaView
from react-native-safe-area-view
for convenience and is functionally equivalent.
SafeAreaView
from react-native
. It's provided by default and works.react-native-safe-area-context
or react-native-safe-area-view
depending on your needs.@react-navigation/native
(v5) / react-navigation
(v4) or react-native-safe-area-view
. It just may work better with React Navigation. Both are equivalent, choose one and use it consistently.I recommend adding an ESLint no-restricted-imports rule that forbids accidentally importing SafeAreaView
from any other location than the one you chose to use.
Example rule allowing import only from from react-navigation
:
'no-restricted-imports': [
'error',
{
paths: [
{
name: 'react-native',
importNames: ['SafeAreaView'],
message: 'Import SafeAreaView from react-navigation instead',
},
{
name: 'react-native-safe-area-context',
importNames: ['SafeAreaView'],
message: 'Import SafeAreaView from react-navigation instead',
},
{
name: 'react-native-safe-area-view',
importNames: ['SafeAreaView'],
message: 'Import SafeAreaView from react-navigation instead',
},
],
},
],
Just some additional information to complement/update @Sampo 's answer:
If you are using react-navigation
v5.x, note that they do not recommend to use their own implementation of SafeAreaView
, but instead to use react-native-safe-area-context
:
While React Native exports a SafeAreaView component, it has some inherent issues, i.e. if a screen containing safe area is animating, it causes jumpy behavior. In addition, this component only supports iOS 10+ with no support for older iOS versions or Android. We recommend to use the react-native-safe-area-context library to handle safe areas in a more reliable way.
Source: https://reactnavigation.org/docs/handling-safe-area/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With