Skip to content

Commit 9be4ce1

Browse files
committed
Drop dangerously prefix in 5.x docs as well
1 parent de5c385 commit 9be4ce1

File tree

7 files changed

+18
-14
lines changed

7 files changed

+18
-14
lines changed

versioned_docs/version-5.x/navigation-events.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Each callback registered as an event listener receive an event object as its arg
2121
- `target` - The route key for the screen that should receive the event. For some events, this maybe `undefined` if the event wasn't related to a specific screen.
2222
- `preventDefault` - For some events, there may be a `preventDefault` method on the event object. Calling this method will prevent the default action performed by the event (such as switching tabs on `tabPress`). Support for preventing actions are only available for certain events like `tabPress` and won't work for all events.
2323

24-
One thing to keep in mind is that you can only listen to events from the immediate parent navigator. For example, if you try to add a listener in a screen is inside a stack that's nested in a tab, it won't get the `tabPress` event. If you need to listen to an event from a parent navigator, you may use `navigation.dangerouslyGetParent()` to get a reference to parent navigator's navigation prop and add a listener.
24+
One thing to keep in mind is that you can only listen to events from the immediate parent navigator. For example, if you try to add a listener in a screen is inside a stack that's nested in a tab, it won't get the `tabPress` event. If you need to listen to an event from a parent navigator, you may use `navigation.getParent()` to get a reference to parent navigator's navigation prop and add a listener.
2525

2626
There are 2 ways to listen to events:
2727

versioned_docs/version-5.x/navigation-prop.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,16 +325,20 @@ Then use it like:
325325
navigation.dispatch(insertBeforeLast('Home'));
326326
```
327327

328-
### `dangerouslyGetParent`
328+
### `getParent`
329329

330-
This method returns the navigation prop from the parent navigator that the current navigator is nested in. For example, if you have a stack navigator and a tab navigator nested inside the stack, then you can use `dangerouslyGetParent` inside a screen of the tab navigator to get the navigation prop passed from the stack navigator.
330+
This method returns the navigation prop from the parent navigator that the current navigator is nested in. For example, if you have a stack navigator and a tab navigator nested inside the stack, then you can use `getParent` inside a screen of the tab navigator to get the navigation prop passed from the stack navigator.
331331

332-
This method will return `undefined` if there is no parent navigator. Be sure to always check for `undefined` when using this method.
332+
It will return `undefined` if there is no parent navigator. Be sure to always check for `undefined` when using this method.
333333

334-
### `dangerouslyGetState`
334+
This is only available in latest versions of `@react-navigation/native`. Earlier, it was named `dangerouslyGetParent`, which is now deprecated.
335+
336+
### `getState`
335337

336338
> Note: Consider the navigator's state object to be internal and subject to change in a minor release. Avoid using properties from the [navigation state](navigation-state.md) object except `index` and `routes`, unless you really need it. If there is some functionality you cannot achieve without relying on the structure of the state object, please open an issue.
337339
338340
This method returns the state object of the navigator which contains the screen. Getting the navigator state could be useful in very rare situations. You most likely don't need to use this method. If you do, make sure you have a good reason.
339341

342+
This is only available in latest versions of `@react-navigation/native`. Earlier, it was named `dangerouslyGetState`, which is now deprecated.
343+
340344
If you need the state for rendering content, you should use [`useNavigationState`](use-navigation-state.md) instead of this method.

versioned_docs/version-5.x/nesting-navigators.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ navigation.dispatch(DrawerActions.toggleDrawer());
8080

8181
For example, if you have a stack navigator nested inside a tab navigator, the screens in the stack navigator won't receive the events emitted by the parent tab navigator such as (`tabPress`) when using `navigation.addListener`.
8282

83-
To receive events from parent navigator, you can explicitly listen to parent's events with `navigation.dangerouslyGetParent()`:
83+
To receive events from parent navigator, you can explicitly listen to parent's events with `navigation.getParent()`:
8484

8585
```js
8686
const unsubscribe = navigation
87-
.dangerouslyGetParent()
87+
.getParent()
8888
.addListener('tabPress', (e) => {
8989
// Do something
9090
});

versioned_docs/version-5.x/stack-actions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ navigation.dispatch({
3939
user: 'jane',
4040
}),
4141
source: route.key,
42-
target: navigation.dangerouslyGetState().key,
42+
target: navigation.getState().key,
4343
});
4444
```
4545

versioned_docs/version-5.x/stack-navigator.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ import { TransitionPresets } from '@react-navigation/stack';
948948
cardOverlayEnabled: true,
949949
headerStatusBarHeight:
950950
navigation
951-
.dangerouslyGetState()
951+
.getState()
952952
.routes.findIndex((r) => r.key === route.key) > 0
953953
? 0
954954
: undefined,

versioned_docs/version-5.x/upgrading-from-4.x.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ See [`pop` action docs](stack-actions.md#pop) for more details.
496496
The `dismiss` method has been removed. You can achieve similar effect with following:
497497
498498
```js
499-
navigation.dangerouslyGetParent().pop();
499+
navigation.getState().pop();
500500
```
501501
502502
### `jumpTo`

versioned_docs/version-5.x/use-navigation-state.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ const state = useNavigationState(state => state);
2222

2323
> Note: This hook is useful for advanced cases and it's easy to introduce performance issues if you're not careful. For most of the cases, you don't need the navigator's state.
2424
25-
## How is `useNavigationState` different from `navigation.dangerouslyGetState()`?
25+
## How is `useNavigationState` different from `navigation.getState()`?
2626

27-
The `navigation.dangerouslyGetState()` function also returns the current [navigation state](navigation-state.md). The main difference is that the `useNavigationState` hook will trigger a re-render when values change, while `navigation.dangerouslyGetState()` won't. For example, the following code will be incorrect:
27+
The `navigation.getState()` function also returns the current [navigation state](navigation-state.md). The main difference is that the `useNavigationState` hook will trigger a re-render when values change, while `navigation.getState()` won't. For example, the following code will be incorrect:
2828

2929
```js
3030
function Profile() {
31-
const routesLength = navigation.dangerouslyGetState().routes.length; // Don't do this
31+
const routesLength = navigation.getState().routes.length; // Don't do this
3232

3333
return <Text>Number of routes: {routesLength}</Text>;
3434
}
@@ -46,7 +46,7 @@ function Profile() {
4646
}
4747
```
4848

49-
So when do you use `navigation.dangerouslyGetState()`? It's mostly useful within event listeners where you don't care about what's rendered. In most cases, using the hook should be preferred.
49+
So when do you use `navigation.getState()`? It's mostly useful within event listeners where you don't care about what's rendered. In most cases, using the hook should be preferred.
5050

5151
## Using with class component
5252

0 commit comments

Comments
 (0)