-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When we have nested navigators, we often may want control over which navigator should handle an action that we dispatch. This is often handled automatically, e.g. if you navigate to a screen named `Settings`, the navigator which contains the `Settings` screen will handle the action. But in some cases, it's not possible to figure out the navigator automatically. Some example cases: We may want to have nested drawers to have a drawer on the left, and one on the right - and still be able to open/close them. We can use `getParent` to achieve this: ```js navigation.getParent().openDrawer(); ``` However, this is prone to error if we nest another navigator inside (e.g. a stack). So we need a way to distinguish between which drawer we want to target. An alternative way to distinguish different navigators is to specify the `key` of the navigator in the `target` field of the action: ```js navigation.dispatch({ ...DrawerActions.openDrawer(), target: 'keyOfTheDrawer', }); ``` However, this has a few problems: - It's not straightforward to get the `key` of the navigator since the `key` is generated by React Navigation. - It's not type-safe. Nothing ensures that the `key` is valid. We may also want to call `setOptions` on a parent navigator. The options don't bubble up unlike the actions, so it's important make sure to call it for the correct navigator. We can use `getParent` in this case as well, but it's prone to the same set of issues as the previous case. There are also no alternatives for this unlike specifying `key` for actions. This commit adds a `id` prop to navigators. Example: ```js <Drawer.Navigator id="LeftDrawer">{/* ... */}</Drawer.Navigator> ``` Then we can use it along with `getParent` as follows: ```js navigation.getParent('LeftDrawer').openDrawer(); ``` No matter how many nested navigators are added, we can always find the correct navigator this way. To make this work with type-checking, we'd pass a third generic to our `NavigationProp` or `ScreenProps` types: ```ts type LeftDrawerScreenProps<T extends keyof LeftDrawerParamList> = DrawerScreenProps<LeftDrawerParamList, T, 'LeftDrawer'>; ``` This isn't the best way to do this, for example, the `id` prop on the navigator itself isn't type-safe. But supporting it with a good API is a lot of work, so for now, I've decided to go with this alternative approach.
- Loading branch information
Showing
22 changed files
with
348 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.