Navigator
suggest changeNavigator is React Native’s built-in stack navigator from this era of the framework. It is deprecated in later releases. A Navigator component manages a stack of route objects and provides methods for managing that stack.
<Navigator
ref={(navigator) => { this.navigator = navigator }}
initialRoute={{ id: 'route1', title: 'Route 1' }}
renderScene={this.renderScene.bind(this)}
configureScene={(route) => Navigator.SceneConfigs.FloatFromRight}
style={{ flex: 1 }}
navigationBar={
// see "Managing the Navigation Bar" below
<Navigator.NavigationBar routeMapper={this.routeMapper} />
}
/>
Managing the Route Stack
Notice the initialRoute prop. A route is a plain JavaScript object. It can take whatever shape and values you need. It is the primary way to pass values and methods between components in the navigation stack.
Navigator renders whatever renderScene returns.
renderScene(route, navigator) {
if (route.id === 'route1') {
return <ExampleScene navigator={navigator} title={route.title} />; // see below
} else if (route.id === 'route2') {
return <ExampleScene navigator={navigator} title={route.title} />; // see below
}
}
An ExampleScene for this setup:
function ExampleScene(props) {
function forward() {
// this route object is passed to the `renderScene` function defined above
props.navigator.push({ id: 'route2', title: 'Route 2' });
}
function back() {
// `pop` removes one route object from the `Navigator` stack
props.navigator.pop();
}
return (
<View>
<Text>{props.title}</Text>
<TouchableOpacity onPress={forward}>
<Text>Go forward!</Text>
</TouchableOpacity>
<TouchableOpacity onPress={back}>
<Text>Go Back!</Text>
</TouchableOpacity>
</View>
);
}
Configuring the Navigator
Configure transitions with the configureScene prop. The function receives the route object and must return a configuration object. These are the built-in configuration objects:
- Navigator.SceneConfigs.PushFromRight (default)
- Navigator.SceneConfigs.FloatFromRight
- Navigator.SceneConfigs.FloatFromLeft
- Navigator.SceneConfigs.FloatFromBottom
- Navigator.SceneConfigs.FloatFromBottomAndroid
- Navigator.SceneConfigs.FadeAndroid
- Navigator.SceneConfigs.HorizontalSwipeJump
- Navigator.SceneConfigs.HorizontalSwipeJumpFromRight
- Navigator.SceneConfigs.VerticalUpSwipeJump
- Navigator.SceneConfigs.VerticalDownSwipeJump
You can return one of these objects unchanged, or copy it and adjust the transition. For example, to widen the edge hit area so it is closer to the iOS UINavigationController interactivePopGestureRecognizer:
configureScene={(route) => {
return {
...Navigator.SceneConfigs.FloatFromRight,
gestures: {
pop: {
...Navigator.SceneConfigs.FloatFromRight.gestures.pop,
edgeHitWidth: Dimensions.get('window').width / 2,
},
},
};
}}
Managing the NavigationBar
The navigationBar prop can take any properly configured React component. The usual implementation is Navigator.NavigationBar. It takes a routeMapper prop that configures the bar for the current route.
A routeMapper is a plain JavaScript object with three functions: Title, RightButton, and LeftButton. For example:
const routeMapper = {
LeftButton(route, navigator, index, navState) {
if (index === 0) {
return null;
}
return (
<TouchableOpacity
onPress={() => navigator.pop()}
style={styles.navBarLeftButton}
>
<Text>Back</Text>
</TouchableOpacity>
);
},
RightButton(route, navigator, index, navState) {
return (
<TouchableOpacity
onPress={route.handleRightButtonClick}
style={styles.navBarRightButton}
>
<Text>Next</Text>
</TouchableOpacity>
);
},
Title(route, navigator, index, navState) {
return (
<Text>{route.title}</Text>
);
},
};
See more
For each prop, see the official React Native documentation for Navigator and the guide on Using Navigators.