|
| 1 | +--- |
| 2 | +id: version-3.x-routers |
| 3 | +title: Routers |
| 4 | +sidebar_label: Routers |
| 5 | +original_id: routers |
| 6 | +--- |
| 7 | + |
| 8 | +Routers define a component's navigation state, and they allow the developer to define paths and actions that can be handled. |
| 9 | + |
| 10 | +## Built-In Routers |
| 11 | + |
| 12 | +`react-navigation` ships with a few standard routers: |
| 13 | + |
| 14 | +- [StackRouter](https://github.com/react-navigation/react-navigation-core/blob/master/src/routers/StackRouter.js) |
| 15 | +- [TabRouter](https://github.com/react-navigation/react-navigation-core/blob/master/src/routers/TabRouter.js) |
| 16 | + |
| 17 | + |
| 18 | +## Using Routers |
| 19 | + |
| 20 | +To make a navigator manually, put a static `router` on a component. |
| 21 | + |
| 22 | +```js |
| 23 | +class MyNavigator extends React.Component { |
| 24 | + static router = StackRouter(routes, config); |
| 25 | + ... |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Now you can use this component as a `screen` in another navigator, and the navigation logic for `MyNavigator` will be defined by this `StackRouter`. |
| 30 | + |
| 31 | +## Customizing Routers |
| 32 | + |
| 33 | +See the [Custom Router API spec](custom-routers.html) to learn about the API of `StackRouter` and `TabRouter`. You can override the router functions as you see fit: |
| 34 | + |
| 35 | +### Custom Navigation Actions |
| 36 | + |
| 37 | +To override navigation behavior, you can override the navigation state logic in `getStateForAction`, and manually manipulate the `routes` and `index`. |
| 38 | + |
| 39 | +```js |
| 40 | +const MyApp = createStackNavigator({ |
| 41 | + Home: { screen: HomeScreen }, |
| 42 | + Profile: { screen: ProfileScreen }, |
| 43 | +}, { |
| 44 | + initialRouteName: 'Home', |
| 45 | +}) |
| 46 | + |
| 47 | +const defaultGetStateForAction = MyApp.router.getStateForAction; |
| 48 | + |
| 49 | +MyApp.router.getStateForAction = (action, state) => { |
| 50 | + if (state && action.type === 'PushTwoProfiles') { |
| 51 | + const routes = [ |
| 52 | + ...state.routes, |
| 53 | + {key: 'A', routeName: 'Profile', params: { name: action.name1 }}, |
| 54 | + {key: 'B', routeName: 'Profile', params: { name: action.name2 }}, |
| 55 | + ]; |
| 56 | + return { |
| 57 | + ...state, |
| 58 | + routes, |
| 59 | + index: routes.length - 1, |
| 60 | + }; |
| 61 | + } |
| 62 | + return defaultGetStateForAction(action, state); |
| 63 | +}; |
| 64 | +``` |
| 65 | + |
| 66 | +### Blocking Navigation Actions |
| 67 | + |
| 68 | +Sometimes you may want to prevent some navigation activity, depending on your route. |
| 69 | + |
| 70 | +```js |
| 71 | +import { NavigationActions } from 'react-navigation' |
| 72 | + |
| 73 | +const MyStackRouter = StackRouter({ |
| 74 | + Home: { screen: HomeScreen }, |
| 75 | + Profile: { screen: ProfileScreen }, |
| 76 | +}, { |
| 77 | + initialRouteName: 'Home', |
| 78 | +}) |
| 79 | + |
| 80 | +const defaultGetStateForAction = MyStackRouter.router.getStateForAction; |
| 81 | + |
| 82 | +MyStackRouter.router.getStateForAction = (action, state) => { |
| 83 | + if ( |
| 84 | + state && |
| 85 | + action.type === NavigationActions.BACK && |
| 86 | + state.routes[state.index].params.isEditing |
| 87 | + ) { |
| 88 | + // Returning null from getStateForAction means that the action |
| 89 | + // has been handled/blocked, but there is not a new state |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + return defaultGetStateForAction(action, state); |
| 94 | +}; |
| 95 | +``` |
| 96 | + |
| 97 | + |
| 98 | +### Handling Custom URIs |
| 99 | + |
| 100 | +Perhaps your app has a unique URI which the built-in routers cannot handle. You can always extend the router `getActionForPathAndParams`. |
| 101 | + |
| 102 | +```js |
| 103 | +import { NavigationActions } from 'react-navigation' |
| 104 | + |
| 105 | +const MyApp = createStackNavigator({ |
| 106 | + Home: { screen: HomeScreen }, |
| 107 | + Profile: { screen: ProfileScreen }, |
| 108 | +}, { |
| 109 | + initialRouteName: 'Home', |
| 110 | +}) |
| 111 | +const previousGetActionForPathAndParams = MyApp.router.getActionForPathAndParams; |
| 112 | + |
| 113 | +Object.assign(MyApp.router, { |
| 114 | + getActionForPathAndParams(path, params) { |
| 115 | + if ( |
| 116 | + path === 'my/custom/path' && |
| 117 | + params.magic === 'yes' |
| 118 | + ) { |
| 119 | + // returns a profile navigate action for /my/custom/path?magic=yes |
| 120 | + return NavigationActions.navigate({ |
| 121 | + routeName: 'Profile', |
| 122 | + action: NavigationActions.navigate({ |
| 123 | + // This child action will get passed to the child router |
| 124 | + // ProfileScreen.router.getStateForAction to get the child |
| 125 | + // navigation state. |
| 126 | + routeName: 'Friends', |
| 127 | + }), |
| 128 | + }); |
| 129 | + } |
| 130 | + return previousGetActionForPathAndParams(path, params); |
| 131 | + }, |
| 132 | +}); |
| 133 | +``` |
0 commit comments