You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: versioned_docs/version-5.x/nesting-navigators.md
+77-2Lines changed: 77 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,6 +48,12 @@ When nesting navigators, there are some things to keep in mind:
48
48
49
49
For example, when you press the back button inside a nested stack navigator, it'll go back to the previous screen inside the nested stack even if there's another navigator as the parent.
50
50
51
+
### Each navigator has it's own options
52
+
53
+
For example, specifying a `title` option in a screen nested in a child navigator won't affect the title shown in a parent navigator.
54
+
55
+
If you want to achieve this behavior, see the guide for [screen options with nested navigators](screen-options-resolution.md#setting-parent-screen-options-based-on-child-navigators-state). this could be useful if you are rendering a tab navigator inside a stack navigator and want to show the title of the active screen inside the tab navigator in the header of the stack navigator.
56
+
51
57
### Navigation actions are handled by current navigator and bubble up if couldn't be handled
52
58
53
59
For example, if you're calling `navigation.goBack()` in a nested screen, it'll only go back in the parent navigator if you're already on the first screen of the navigator. Other actions such as `navigate` work similarly, i.e. navigation will happen in the nested navigator and if the nested navigator couldn't handle it, then the parent navigator will try to handle it. In the above example, when calling `navigate('Messages')`, inside `Feed` screen, the nested tab navigator will handle it, but if you call `navigate('Settings')`, the parent stack navigator will handle it.
It's sometimes useful to nest multiple stack navigators, for example, to have [some screens in a modal stack and some in regular stack](modal.md).
172
+
173
+
When nesting multiple stacks, React Navigation will automatically hide the header from the child stack navigator in order to avoid showing duplicate headers. However, depending on the scenario, it might be more useful to show the header in the child stack navigator instead and hide the header in the parent stack navigator.
174
+
175
+
To achieve this, you can hide the header in the screen containing the stack using the `headerShown: false` option.
A complete example can be found in the [modal guide](modal.md). However, the principle isn't only specific to modals, but any kind of nesting of stack navigators.
206
+
207
+
In rare cases, you also might want to show both headers from the child and parent stack navigators. In this case, you can explicitly use `headerShown: true` on the child stack navigator to override the default behavior.
In these examples, we have used a stack navigator directly nested inside another stack navigator, but the same principle applies when there are other navigators in the middle, for example: stack navigator inside a tab navigator which is inside another stack navigator.
234
+
235
+
When nesting multiple stack navigators, we recommend nesting at most 2 stack navigators, unless absolutely necessary.
236
+
163
237
## Best practices when nesting
164
238
165
239
We recommend to reduce nesting navigators to minimal. Try to achieve the behavior you want with as little nesting as possible. Nesting has many downsides:
166
240
167
-
- Code becomes difficult to follow when navigating to nested screens
168
241
- It results in deeply nested view hierarchy which can cause memory and performance issues in lower end devices
169
-
- Nesting same type of navigators (e.g. tabs inside tabs, drawer inside drawer etc.) leads to a confusing UX
242
+
- Nesting same type of navigators (e.g. tabs inside tabs, drawer inside drawer etc.) might lead to a confusing UX
243
+
- With excessive nesting, code becomes difficult to follow when navigating to nested screens, configuring deep link etc.
170
244
171
245
Think of nesting navigators as a way to achieve the UI you want rather than a way to organize your code. If you want to create separate group of screens for organization, instead of using separate navigators, consider doing something like this:
172
246
@@ -187,6 +261,7 @@ const userScreens = {
187
261
};
188
262
189
263
// Then use them in your components by looping over the object and creating screen configs
264
+
// You could extract this logic to a utility function and reuse it to simplify your code
0 commit comments