2017-01-26 11:49:39 -08:00
# StackNavigator
Provides a way for your app to transition between screens where each new screen is placed on top of a stack.
By default the StackNavigator is configured to have the familiar iOS and Android look & feel: new screens slide in from the right on iOS, fade in from the bottom on Android. On iOS the StackNavigator can also be configured to a modal style where screens slide in from the bottom.
```jsx
class MyHomeScreen extends React.Component {
static navigationOptions = {
title: 'Home',
}
render() {
return (
< Button
onPress={() => this.props.navigation.navigate('Profile', {name: 'Lucy'})}
2017-02-08 22:32:26 -05:00
title="Go to Lucy's profile"
2017-01-26 11:49:39 -08:00
/>
);
}
}
const ModalStack = StackNavigator({
Home: {
screen: MyHomeScreen,
},
Profile: {
path: 'people/:name',
screen: MyProfileScreen,
},
});
```
## API Definition
```js
StackNavigator(RouteConfigs, StackNavigatorConfig)
```
### RouteConfigs
The route configs object is a mapping from route name to a route config, which tells the navigator what to present for that route.
```js
StackNavigator({
// For each screen that you can navigate to, create a new entry like this:
Profile: {
// `ProfileScreen` is a React component that will be the main content of the screen.
screen: ProfileScreen,
2017-02-02 05:27:05 +08:00
// When `ProfileScreen` is loaded by the StackNavigator, it will be given a `navigation` prop.
2017-01-26 11:49:39 -08:00
// Optional: When deep linking or using react-navigation in a web app, this path is used:
path: 'people/:username',
// The action and route params are extracted from the path.
2017-02-02 05:27:05 +08:00
// Optional: Override the `navigationOptions` for the screen
2017-04-13 00:49:08 +02:00
navigationOptions: ({navigation}) => ({
title: `${navigation.state.params.username}'s Profile'` ,
}),
2017-01-26 11:49:39 -08:00
},
...MyOtherRoutes,
});
```
### StackNavigatorConfig
Options for the router:
- `initialRouteName` - Sets the default screen of the stack. Must match one of the keys in route configs.
- `initialRouteParams` - The params for the initial route
2017-01-27 12:33:03 +01:00
- `navigationOptions` - Default navigation options to use for screens
2017-01-26 11:49:39 -08:00
- `paths` - A mapping of overrides for the paths set in the route configs
Visual options:
- `mode` - Defines the style for rendering and transitions:
- `card` - Use the standard iOS and Android screen transitions. This is the default.
- `modal` - Make the screens slide in from the bottom which is a common iOS pattern. Only works on iOS, has no effect on Android.
- `headerMode` - Specifies how the header should be rendered:
- `float` - Render a single header that stays at the top and animates as screens are changed. This is a common pattern on iOS.
- `screen` - Each screen has a header attached to it and the header fades in and out together with the screen. This is a common pattern on Android.
- `none` - No header will be rendered.
2017-02-02 05:31:24 +02:00
- `cardStyle` - Use this prop to override or extend the default style for an individual card in stack.
2017-04-24 20:31:44 +08:00
- `transitionConfig` - Function to return an object that overrides default screen transitions.
2017-02-08 18:53:41 +02:00
- `onTransitionStart` - Function to be invoked when the card transition animation is about to start.
- `onTransitionEnd` - Function to be invoked once the card transition animation completes.
2017-01-26 11:49:39 -08:00
### Screen Navigation Options
2017-04-13 00:49:08 +02:00
#### `title`
2017-01-26 11:49:39 -08:00
2017-05-02 15:48:32 -04:00
String that can be used as a fallback for `headerTitle` and `tabBarLabel`
2017-01-26 11:49:39 -08:00
2017-04-26 13:34:21 +02:00
#### `header`
2017-01-26 11:49:39 -08:00
2017-04-26 13:34:21 +02:00
React Element or a function that given `HeaderProps` returns a React Element, to display as a header. Setting to `null` hides header.
2017-04-13 00:49:08 +02:00
#### `headerTitle`
String or React Element used by the header. Defaults to scene `title`
#### `headerBackTitle`
Title string used by the back button on iOS or `null` to disable label. Defaults to scene `title`
2017-04-22 09:26:31 +03:00
#### `headerTruncatedBackTitle`
Title string used by the back button when `headerBackTitle` doesn't fit on the screen. `"Back"` by default.
2017-04-13 00:49:08 +02:00
#### `headerRight`
2017-04-24 19:47:42 +02:00
React Element to display on the right side of the header
2017-04-13 00:49:08 +02:00
#### `headerLeft`
2017-04-24 19:47:42 +02:00
React Element to display on the left side of the header
2017-04-13 00:49:08 +02:00
#### `headerStyle`
Style object for the header
#### `headerTitleStyle`
Style object for the title component
2017-04-28 07:55:15 +02:00
#### `headerBackTitleStyle`
Style object for the back title
2017-04-13 00:49:08 +02:00
#### `headerTintColor`
Tint color for the header
#### `headerPressColorAndroid`
Color for material ripple (Android >= 5.0 only)
#### `gesturesEnabled`
2017-01-26 11:49:39 -08:00
2017-04-26 13:55:25 +01:00
Whether you can use gestures to dismiss this screen. Defaults to true on iOS, false on Android.
2017-01-26 11:49:39 -08:00
2017-02-02 05:27:05 +08:00
### Navigator Props
2017-01-26 11:49:39 -08:00
2017-02-02 05:27:05 +08:00
The navigator component created by `StackNavigator(...)` takes the following props:
2017-01-26 11:49:39 -08:00
2017-02-02 05:27:05 +08:00
- `screenProps` - Pass down extra options to child screens, for example:
2017-01-26 11:49:39 -08:00
2017-02-02 05:27:05 +08:00
```jsx
const SomeStack = StackNavigator({
// config
});
2017-02-08 18:53:41 +02:00
2017-02-02 05:27:05 +08:00
< SomeStack
2017-02-09 00:30:09 +05:30
screenProps={/* this prop will get passed to the screen components as this.props.screenProps */}
2017-02-02 05:27:05 +08:00
/>
```
2017-02-08 18:53:41 +02:00
2017-02-02 05:27:05 +08:00
### Examples
2017-01-26 11:49:39 -08:00
2017-02-02 05:27:05 +08:00
See the examples [SimpleStack.js ](https://github.com/react-community/react-navigation/tree/master/examples/NavigationPlayground/js/SimpleStack.js ) and [ModalStack.js ](https://github.com/react-community/react-navigation/tree/master/examples/NavigationPlayground/js/ModalStack.js ) which you can run locally as part of the [NavigationPlayground ](https://github.com/react-community/react-navigation/tree/master/examples/NavigationPlayground ) app.