Custom routers
The router object provides various helper methods to deal with the state and actions, a reducer to update the state as well as some action creators.
The router is responsible for handling actions dispatched by calling methods on the navigation object. If the router cannot handle an action, it can return null
, which would propagate the action to other routers until it's handled.
You can make your own router by building an object with the following functions:
type
- String representing the type of the router, e.g.'stack'
,'tab'
,'drawer'
etc.getInitialState
- Function which returns the initial state for the navigator. Receives an options object withrouteNames
androuteParamList
properties.getRehydratedState
- Function which rehydrates the full navigation state from a given partial state. Receives a partial state object and an options object withrouteNames
androuteParamList
properties.getStateForRouteNamesChange
- Function which takes the current state and updated list of route names, and returns a new state. Receives the state object and an options object withrouteNames
androuteParamList
properties.getStateForAction
- function which takes the current state and action along with an options object withrouteNames
androuteParamList
properties, and returns a new state. If the action cannot be handled, it should returnnull
.getStateForRouteFocus
- Function which takes the current state and key of a route, and returns a new state with that route focused.shouldActionChangeFocus
- Function which determines whether the action should also change focus in parent navigator. Some actions such asNAVIGATE
can change focus in the parent.actionCreators
- Optional object containing a list of action creators, such aspush
,pop
etc. These will be used to add helper methods to thenavigation
object to dispatch those actions.
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 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 |
|
Built-In Routers¶
The library ships with a few standard routers:
StackRouter
TabRouter
DrawerRouter
Customizing Routers¶
You can reuse a router and override the router functions as per your needs, such as customizing how existing actions are handled, adding additional actions etc.
See custom navigators for details on how to override the router with a custom router in an existing navigator.
Custom Navigation Actions¶
Let's say you want to add a custom action to clear the history:
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 |
|
Instead of writing a custom router to handle custom actions, you can pass a function to dispatch
instead. It's cleaner and recommended instead of overriding routers.
Blocking Navigation Actions¶
Sometimes you may want to prevent some navigation activity, depending on your route. Let's say, you want to prevent pushing a new screen if isEditing
is true
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
If you want to prevent going back, the recommended approach is to use the beforeRemove
event.