Skip to content

Commit 17fc644

Browse files
committed
Get rid of all references to route.state
1 parent cdcd23e commit 17fc644

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

blog/2020-01-29-using-react-navigation-5-with-react-native-paper.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,7 @@ const Tab = createMaterialBottomTabNavigator();
690690

691691
export const BottomTabs = props => {
692692
// Get a name of current screen
693-
const routeName = props.route.state
694-
? props.route.state.routes[props.route.state.index].name
695-
: 'Feed';
696-
693+
const routeName = getFocusedRouteNameFromRoute(route) ?? 'Feed';
697694
const isFocused = useIsFocused();
698695

699696
let icon = 'feather';

versioned_docs/version-5.x/screen-options-resolution.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export default function App() {
115115

116116
If we were to set the `headerTitle` with `options` for the `FeedScreen`, this would not work. This is because `App` stack will only look at its immediate children for configuration: `HomeTabs` and `SettingsScreen`.
117117

118-
But we can determine the `headerTitle` option based on the navigation state of our tab navigator using the `route.state` property. Let's create a function to get the title from `route.state` first:
118+
But we can determine the `headerTitle` option based on the navigation state of our tab navigator using the `getFocusedRouteNameFromRoute` property. Let's create a function to get the title first:
119119

120120
```js
121121
import { getFocusedRouteNameFromRoute } from '@react-navigation/native';
@@ -171,9 +171,7 @@ Then we can use this function in 2 ways:
171171
}
172172
```
173173
174-
So what's happening here? The `route` prop contains a `state` property which refers to the child navigator's state (in this case it's the tab navigator since that's what we're rendering). We are getting the value of the currently active route name from this state and setting an appropriate title for the header.
175-
176-
> Note: The `route.state` property may not exist at all. This will always happen if we have never navigated inside the tab navigator. So it's very important to handle this case, otherwise, your app will crash.
174+
So what's happening here? With the `getFocusedRouteNameFromRoute` helper, we can get the currently active route name from this child navigator (in this case it's the tab navigator since that's what we're rendering) and setting an appropriate title for the header.
177175
178176
This approach can be used anytime you want to set options for a parent navigator based on a child navigator's state. Common use cases are:
179177

0 commit comments

Comments
 (0)