Skip to content

Commit a849eef

Browse files
nargovskevy
authored andcommitted
Add doc note for nesting wrapped navigator (react-navigation#1947)
After having to look for a solution for nesting a navigator embedded in a component, found this issue that dealt with the same problem: react-navigation#90. Added the solution to the guide as a side note.
1 parent be1da2c commit a849eef

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

docs/guides/Guide-Nested.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,43 @@ Now we have put one navigator inside another, and we can `navigate` between navi
7272
```phone-example
7373
nested
7474
```
75+
76+
## Nesting a Navigator in a Component
77+
Sometimes it is desirable to nest a navigator that is wrapped in a component. This is useful in cases where the navigator only takes up part of the screen. For the child navigator to be wired into the navigation tree, it needs the `navigation` property from the parent navigator.
78+
79+
```js
80+
const SimpleApp = StackNavigator({
81+
Home: { screen: NavigatorWrappingScreen },
82+
Chat: { screen: ChatScreen },
83+
});
84+
```
85+
In this case, the NavigatorWrappingScreen is not a navigator, but it renders a navigator as part of its output.
86+
87+
```js
88+
class NavigatorWrappingScreen extends React.Component {
89+
render() {
90+
return (
91+
<View>
92+
<SomeComponent/>
93+
<MainScreenNavigator/>
94+
</View>
95+
);
96+
}
97+
}
98+
```
99+
100+
To wire `MainScreenNavigator` into the navigation tree, we assign its `router` to the wrapping component. This makes `NavigatorWrappingScreen` "navigation aware", which tells the parent navigator to pass the navigation object down. Since the `NavigatorWrappingScreen`'s `router` is overridden with the child navigator's `router`, the child navigator will receive the needed `navigation`.
101+
102+
```js
103+
class NavigatorWrappingScreen extends React.Component {
104+
render() {
105+
return (
106+
<View>
107+
<SomeComponent/>
108+
<MainScreenNavigator navigation={this.props.navigation}/>
109+
</View>
110+
);
111+
}
112+
}
113+
NavigatorWrappingScreen.router = MainScreenNavigator.router;
114+
```

0 commit comments

Comments
 (0)