Upgrading from 3.x
In React Navigation 4, we've extracted out the navigators to separate packages to make it easier to maintain and release updates faster. You can follow the guide below to upgrade your projects.
Note: Before making these changes, we recommend you to commit all local changes to git so you can revert back to a good state if something goes wrong with the upgrade.
Install the new packages
First, we need to install the react-navigation
package along with the various navigators. If you don't use some of these navigators, you can omit them.
To install them, run:
- npm
- Yarn
- pnpm
npm install react-navigation react-navigation-stack@^1.7.3 react-navigation-tabs@^1.2.0 react-navigation-drawer@^1.4.0
yarn add react-navigation react-navigation-stack@^1.7.3 react-navigation-tabs@^1.2.0 react-navigation-drawer@^1.4.0
pnpm add react-navigation react-navigation-stack@^1.7.3 react-navigation-tabs@^1.2.0 react-navigation-drawer@^1.4.0
This will install the versions compatible with your code if you were using [email protected]
, so you wouldn't need any more changes beyond changing the imports.
Note: If you have
@react-navigation/core
or@react-navigation/native
in yourpackage.json
, please remove them and change the imports to import fromreact-navigation
package instead.
Changing your code
Then change any imports for stack, tabs or drawer to import from the above packages instead of react-navigation
.
- import { createAppContainer, createStackNavigator } from 'react-navigation';
+ import { createAppContainer } from 'react-navigation';
+ import { createStackNavigator } from 'react-navigation-stack';
The following imports need to be changed to import from react-navigation-stack
:
createStackNavigator
StackGestureContext
Transitioner
StackView
StackViewCard
StackViewTransitionConfigs
Header
HeaderTitle
HeaderBackButton
HeaderStyleInterpolator
The following imports need to be changed to import from react-navigation-tabs
:
createBottomTabNavigator
createMaterialTopTabNavigator
BottomTabBar
MaterialTopTabBar
The following imports need to be changed to import from react-navigation-drawer
:
createDrawerNavigator
DrawerGestureContext
DrawerRouter
DrawerActions
DrawerView
DrawerNavigatorItems
DrawerSidebar
Upgrading navigators (optional)
You don't need to upgrade the navigators to their latest version when upgrading to [email protected]
. You can upgrade them separately later as per your convenience.
Note: We recommend to do each of these changes in a separate commit so you can revert back to a good state if something goes wrong with the upgrade.
Installing dependencies
The latest drawer and tabs depend on react-native-gesture-handler
and react-native-reanimated
. If you already have these libraries installed and at the latest version, you are done here! Otherwise, read on for installation instructions for these dependencies.
Installing dependencies into an Expo managed project
In your project directory, run the following:
npx expo install react-native-gesture-handler react-native-reanimated
This will install versions of these libraries that are compatible.
Installing dependencies into a bare React Native project
In your project directory, run npm install react-native-reanimated react-native-gesture-handler react-native-screens
.
Next, we need to link these libraries. The steps depends on your React Native version:
-
React Native 0.60 and higher
On newer versions of React Native, linking is automatic.
To complete the linking on iOS, make sure you have Cocoapods installed. Then run:
cd ios
pod install
cd .. -
React Native 0.59 and lower
If you're on an older React Native version, you need to manually link the dependencies. To do that, run:
react-native link react-native-reanimated
react-native link react-native-gesture-handler
To finalize installation of react-native-gesture-handler
for Android, make the following modifications to MainActivity.java
:
package com.reactnavigation.example;
import com.facebook.react.ReactActivity;
+ import com.facebook.react.ReactActivityDelegate;
+ import com.facebook.react.ReactRootView;
+ import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
public class MainActivity extends ReactActivity {
@Override
protected String getMainComponentName() {
return "Example";
}
+ @Override
+ protected ReactActivityDelegate createReactActivityDelegate() {
+ return new ReactActivityDelegate(this, getMainComponentName()) {
+ @Override
+ protected ReactRootView createRootView() {
+ return new RNGestureHandlerEnabledRootView(MainActivity.this);
+ }
+ };
+ }
}
Upgrading packages
react-navigation-tabs
To upgrade react-navigation-tabs
, run:
- npm
- Yarn
- pnpm
npm install react-navigation-tabs
yarn add react-navigation-tabs
pnpm add react-navigation-tabs
This version upgrades react-native-tab-view
to 2.x. As a result, the animations in createMaterialTopTabNavigator
now use the react-native-reanimated
library.
Breaking changes
- If you have a custom tab bar in
createMaterialTopTabNavigator
which uses theposition
prop, you'll need to update it to useAnimated
fromreact-native-reanimated
instead ofreact-native
. - The
activeTintColor
andinactiveTintColor
options for the tab bar ofcreateMaterialTopTabNavigator
now controls the opacity of the label and icons as well. - The
animationsEnabled
andoptimizationsEnabled
options have been removed fromcreateMaterialTopTabNavigator
. - Support for React < 16.3 has been dropped, which means the minimum supported React Native version is now 0.56.
New features
- A new
lazyPlaceholderComponent
option is added which lets you show a placeholder for lazy loaded tabs.
react-navigation-drawer
To upgrade react-navigation-drawer
, run:
- npm
- Yarn
- pnpm
npm install react-navigation-drawer
yarn add react-navigation-drawer
pnpm add react-navigation-drawer
This version upgrades now uses the react-native-reanimated
library for animations. This means, if you're using the drawerProgress
value, you'll need to migrate your code to use Animated
from react-native-reanimated
.
react-navigation-stack
To upgrade react-navigation-stack
, run:
- npm
- Yarn
- pnpm
npm install react-navigation-stack
yarn add react-navigation-stack
pnpm add react-navigation-stack
In this release, we have moved several options into navigationOptions
so that you can configure options per screen instead of per navigator. This lets you do things like customize animations for a particular screen, set options based on screenProps
etc. Usage of built-in components such as Header
and HeaderBackButton
has also been simplified. Other changes are made to improve consistency within the API.
From this version, all state changes have an animation, including replace
and reset
which didn't do an animation previously. If you don't want animations, you can specify animationEnabled: false
in navigationOptions
for a specific screen, or in defaultNavigationOptions
for the whole navigator.
Note: The alpha versions for 2.0 used Reanimated for the animations. We've replaced Reanimated with React Native's Animated API in the stable release. If you did any custom animations with the alpha, please migrate your code to the Animated API.
New peer dependencies
The new version requires 2 new peer dependencies. To install them in your project, run:
- npm
- Yarn
- pnpm
npm install react-native-safe-area-context @react-native-community/masked-view
yarn add react-native-safe-area-context @react-native-community/masked-view
pnpm add react-native-safe-area-context @react-native-community/masked-view
Stack Navigator config
The following configuration options have been removed or moved:
cardShadowEnabled
- moved tonavigationOptions
cardOverlayEnabled
- moved tonavigationOptions
cardStyle
- moved tonavigationOptions
transparentCard
- removed in favor ofcardStyle: { backgroundColor: 'transparent' }
innavigationOptions
headerBackTitleVisible
- moved tonavigationOptions
headerLayoutPreset
- moved tonavigationOptions
asheaderTitleAlign
onTransitionStart
- moved tonavigationOptions
onTransitionEnd
- moved tonavigationOptions
headerTransitionPreset
- removed in favor of new APIs for animations innavigationOptions
transitionConfig
- removed in favor of new APIs for animations innavigationOptions
navigationOptions
The following navigationOptions
have been removed or changed:
headerForceInset
- usesafeAreaInsets
instead to control the safe areas, or useheaderStatusBarHeight
to control the padding for the status bar.gesturesEnabled
- renamed togestureEnabled
for consistency.header
- now accepts a function returning react element instead, useheaderShown: false
instead ofheader: null
to hide the header.headerTitle
- now accepts a function returning a React element or a string.headerLeft
- now accepts a function returning a React element.headerRight
- now accepts a function returning a React element.headerBackImage
- now accepts a function returning a React element.headerBackTitle
- now specifies the back title visible in current screen instead of next, specifyingnull
no longer hides back title, usebackTitleVisible
instead, for a screen to change next screen's back title, it can pass params.headerBackground
- now accepts a function returning a React element.
The following navigationOptions
have been added:
gestureEnabled
animationEnabled
headerTitleAlign
cardShadowEnabled
cardOverlayEnabled
cardStyle
headerBackgroundStyle
headerBackTitleVisible
swipeVelocityImpact
onTransitionStart
onTransitionEnd
You can find more details about these options in the documentation.
Library exports
The library now exports the following items:
createStackNavigator
StackView
Header
HeaderTitle
HeaderBackButton
HeaderBackground
CardStyleInterpolators
HeaderStyleInterpolators
TransitionSpecs
TransitionPresets
CardAnimationContext
GestureHandlerRefContext
HeaderHeightContext
useCardAnimation
useHeaderHeight
useGestureHandlerRef
The following components now receive different set of props, so if you use them, or use your own custom component, you will need to update them:
Header
(header
option)
mode
layout
scene
previous
navigation
styleInterpolator
HeaderBackButton
(headerLeft
option)
disabled
onPress
pressColorAndroid
backImage
tintColor
label
truncatedLabel
labelVisible
labelStyle
allowFontScaling
onLabelLayout
screenLayout
titleLayout
canGoBack
Removal of Transitioner
The old Transitioner
component has been removed as a result of rewrite of the animation logic. We're not going to expose the new animation logic since it's internal implementation detail and we want to be able to change it without breaking your code. If you need Transitioner
in your project for some reason, you can copy the old files into your project Transitioner.tsx.
TypeScript
If you're using TypeScript, you'll also need to upgrade the navigators to the latest version following the previous section. Since the navigators have been extracted out, navigator specific types have been removed from the main package. You'll need to update the types accordingly:
- Replace
NavigationScreenProp
with:NavigationSwitchProp
forcreateSwitchNavigator
fromreact-navigation
NavigationStackProp
forcreateStackNavigator
fromreact-navigation-stack
NavigationTabProp
forcreateBottomTabNavigator
andcreateMaterialTopTabNavigator
fromreact-navigation-tabs
NavigationDrawerProp
forcreateDrawerNavigator
fromreact-navigation-drawer
- Replace
NavigationScreenProps
with:NavigationSwitchScreenProps
forcreateSwitchNavigator
fromreact-navigation
NavigationStackScreenProps
forcreateStackNavigator
fromreact-navigation-stack
NavigationTabScreenProps
forcreateBottomTabNavigator
andcreateMaterialTopTabNavigator
fromreact-navigation-tabs
NavigationDrawerScreenProps
forcreateDrawerNavigator
fromreact-navigation-drawer
- Replace
NavigationScreenOptions
with:NavigationStackOptions
forcreateStackNavigator
fromreact-navigation-stack
NavigationBottomTabOptions
forcreateBottomTabNavigator
fromreact-navigation-tabs
NavigationMaterialTabOptions
forcreateMaterialTopTabNavigator
fromreact-navigation-tabs
NavigationDrawerOptions
forcreateDrawerNavigator
fromreact-navigation-drawer
- Replace
NavigationScreenComponent
with:NavigationSwitchScreenComponent
forcreateSwitchNavigator
fromreact-navigation
NavigationStackScreenComponent
forcreateStackNavigator
fromreact-navigation-stack
NavigationBottomTabScreenComponent
forcreateBottomTabNavigator
fromreact-navigation-tabs
NavigationMaterialTabScreenComponent
forcreateMaterialTopTabNavigator
fromreact-navigation-tabs
NavigationDrawerScreenComponent
forcreateDrawerNavigator
fromreact-navigation-drawer
See the TypeScript guide for more details.
TypeScript support is still a work in progress, so please open an issue if you're facing a problem.