Navigator
handles the transition between different scenes in your app.
It is implemented in JavaScript and is available on both iOS and Android. If
you are targeting iOS only, you may also want to consider using
NavigatorIOS
as it leverages native UIKit
navigation.
To set up the Navigator
you provide one or more objects called routes,
to identify each scene. You also provide a renderScene
function that
renders the scene for each route object.
In the above example, initialRoute
is used to specify the first route. It
contains a title
property that identifies the route. The renderScene
prop returns a function that displays text based on the route's title.
The first example demonstrated one scene. To set up multiple scenes, you pass
the initialRouteStack
prop to Navigator
:
In the above example, a routes
variable is defined with two route objects
representing two scenes. Each route has an index
property that is used to
manage the scene being rendered. The renderScene
method is changed to
either push or pop the navigator depending on the current route's index.
Finally, the Text
component in the scene is now wrapped in a
TouchableHighlight
component to help trigger the navigator transitions.
You can optionally pass in your own navigation bar by returning a
Navigator.NavigationBar
component to the navigationBar
prop in
Navigator
. You can configure the navigation bar properties, through
the routeMapper
prop. There you set up the left, right, and title
properties of the navigation bar:
When configuring the left, right, and title items for the navigation bar, you have access to info such as the current route object and navigation state. This allows you to customize the title for each scene as well as the buttons. For example, you can choose to hide the left button for one of the scenes.
Typically you want buttons to represent the left and right buttons. Building on the previous example, you can set this up as follows:
This sets up a left navigator bar button that's visible on scenes after the the first one. When the button is tapped the navigator is popped.
Another type of navigation bar, with breadcrumbs, is provided by
Navigator.BreadcrumbNavigationBar
. You can also provide your own navigation
bar by passing it through the navigationBar
prop. See the
UIExplorer
demo to try out both built-in navigation bars out and see how to use them.
To change the animation or gesture properties of the scene, provide a
configureScene
prop to get the config object for a given route:
In the above example, the newly pushed scene will float up from the bottom.
See Navigator.SceneConfigs
for default animations and more info on
available scene config options.
Optional function where you can configure scene animations and
gestures. Will be invoked with route
and routeStack
parameters,
where route
corresponds to the current scene being rendered by the
Navigator
and routeStack
is the set of currently mounted routes
that the navigator could transition to.
The function should return a scene configuration object.
Available scene configuration options are:
The initial route for navigation. A route is an object that the navigator will use to identify each scene it renders.
If both initialRoute
and initialRouteStack
props are passed to
Navigator
, then initialRoute
must be in a route in
initialRouteStack
. If initialRouteStack
is passed as a prop but
initialRoute
is not, then initialRoute
will default internally to
the last item in initialRouteStack
.
Pass this in to provide a set of routes to initially mount. This prop
is required if initialRoute
is not provided to the navigator. If this
prop is not passed in, it will default internally to an array
containing only initialRoute
.
Use this to provide an optional component representing a navigation bar
that is persisted across scene transitions. This component will receive
two props: navigator
and navState
representing the navigator
component and its state. The component is re-rendered when the route
changes.
Optionally pass in the navigator object from a parent Navigator
.
Will be called with the new route of each scene after the transition is complete or after the initial mounting.
Pass in a function to get notified with the target route when the navigator component is mounted and before each navigator transition.
Required function which renders the scene for a given route. Will be
invoked with the route
and the navigator
object.
Styles to apply to the container of each scene.
Reset every scene with an array of routes.
Name and Type | Description |
---|---|
nextRouteStack RouteStack | Next route stack to reinitialize. All existing route stacks are destroyed and potentially recreated. There is no accompanying animation and this method immediately replaces and re-renders the navigation bar and the stack items. |
Transition to an existing scene without unmounting.
Name and Type | Description |
---|---|
route object | Route to transition to. The specified route must
be in the currently mounted set of routes defined in |
Jump forward to the next scene in the route stack.
Jump backward without unmounting the current scene.
Navigate forward to a new scene, squashing any scenes that you could jump forward to.
Name and Type | Description |
---|---|
route object | Route to push into the navigator stack. |
Go back N scenes at once. When N=1, behavior matches pop()
.
When N is invalid(negative or bigger than current routes count), do nothing.
Name and Type | Description |
---|---|
n number | The number of scenes to pop. Should be an integer. |
Transition back and unmount the current scene.
Replace a scene as specified by an index.
Name and Type | Description |
---|---|
route object | Route representing the new scene to render. |
index number | The route in the stack that should be replaced. If negative, it counts from the back of the stack. |
cb Function | Callback function when the scene has been replaced. |
Replace the current scene with a new route.
Name and Type | Description |
---|---|
route object | Route that replaces the current scene. |
Replace the previous scene.
Name and Type | Description |
---|---|
route object | Route that replaces the previous scene. |
Pop to the first scene in the stack, unmounting every other scene.
Pop to a particular scene, as specified by its route. All scenes after it will be unmounted.
Name and Type | Description |
---|---|
route object | Route to pop to. |
Replace the previous scene and pop to it.
Name and Type | Description |
---|---|
route object | Route that replaces the previous scene. |
Navigate to a new scene and reset route stack.
Name and Type | Description |
---|---|
route object | Route to navigate to. |
Returns the current list of routes.
You can edit the content above on GitHub and send us a pull request!