Custom navigators
Navigators allow you to define your application's navigation structure. Navigators also render common elements such as headers and tab bars which you can configure.
Under the hood, navigators are plain React components.
Built-in Navigators¶
We include some commonly needed navigators such as:
createStackNavigator
- Renders one screen at a time and provides transitions between screens. When a new screen is opened it is placed on top of the stack.createDrawerNavigator
- Provides a drawer that slides in from the left of the screen by default.createBottomTabNavigator
- Renders a tab bar that lets the user switch between several screens.createMaterialTopTabNavigator
- Renders tab view which lets the user switch between several screens using swipe gesture or the tab bar.createMaterialBottomTabNavigator
- Renders tab view which lets the user switch between several screens using swipe gesture or the tab bar.
API for building custom navigators¶
A navigator bundles a router and a view which takes the navigation state and decides how to render it. We export a useNavigationBuilder
hook to build custom navigators that integrate with rest of React Navigation.
useNavigationBuilder
¶
This hook allows a component to hook into React Navigation. It accepts the following arguments:
createRouter
- A factory method which returns a router object (e.g.StackRouter
,TabRouter
).-
options
- Options for the hook and the router. The navigator should forward its props here so that user can provide props to configure the navigator. By default, the following options are accepted: -
children
(required) - Thechildren
prop should contain route configurations asScreen
components. screenOptions
- ThescreenOptions
prop should contain default options for all of the screens.initialRouteName
- TheinitialRouteName
prop determines the screen to focus on initial render. This prop is forwarded to the router.
If any other options are passed here, they'll be forwarded to the router.
The hook returns an object with following properties:
state
- The navigation state for the navigator. The component can take this state and decide how to render it.navigation
- The navigation object containing various helper methods for the navigator to manipulate the navigation state. This isn't the same as the navigation object for the screen and includes some helpers such asemit
to emit events to the screens.-
descriptors
- This is an object containing descriptors for each route with the route keys as its properties. The descriptor for a route can be accessed bydescriptors[route.key]
. Each descriptor contains the following properties: -
navigation
- The navigation prop for the screen. You don't need to pass this to the screen manually. But it's useful if we're rendering components outside the screen that need to receivenavigation
prop as well, such as a header component. options
- A getter which returns the options such astitle
for the screen if they are specified.render
- A function which can be used to render the actual screen. Callingdescriptors[route.key].render()
will return a React element containing the screen content. It's important to use this method to render a screen, otherwise any child navigators won't be connected to the navigation tree properly.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
The navigation
object for navigators also has an emit
method to emit custom events to the child screens. The usage looks like this:
1 2 3 4 5 |
|
The data
is available under the data
property in the event
object, i.e. event.data
.
The target
property determines the screen that will receive the event. If the target
property is omitted, the event is dispatched to all screens in the navigator.
createNavigatorFactory
¶
This createNavigatorFactory
function is used to create a function that will Navigator
and Screen
pair. Custom navigators need to wrap the navigator component in createNavigatorFactory
before exporting.
Example:
1 2 3 4 5 6 7 8 |
|
Then it can be used like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Type-checking navigators¶
To type-check navigators, we need to provide 3 types:
- Type of the props accepted by the view
- Type of supported screen options
- A map of event types emitted by the navigator
For example, to type-check our custom tab navigator, we can do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
Extending Navigators¶
All of the built-in navigators export their views, which we can reuse and build additional functionality on top of them. For example, if we want to re-build the bottom tab navigator, we need the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
Now, we can customize it to add additional functionality or change the behavior. For example, use a custom router instead of the default TabRouter
:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|