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.
getInitialState- Function which returns the initial state for the navigator. Receives an options object with
getRehydratedState- Function which rehydrates the full navigation state from a given partial state. Receives a partial state object and an options object with
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 with
getStateForAction- function which takes the current state and action along with an options object with
routeParamListproperties, and returns a new state. If the action cannot be handled, it should return
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 as
NAVIGATEcan change focus in the parent.
actionCreators- Optional object containing a list of action creators, such as
popetc. These will be used to add helper methods to the
navigationobject to dispatch those actions.
The library ships with a few standard 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:
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
If you want to prevent going back, the recommended approach is to use the