Skip to content

Commit 6bcd73e

Browse files
committed
Document getDrawerStatusFromState
1 parent 78794e6 commit 6bcd73e

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

versioned_docs/version-6.x/drawer-navigator.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ It supports the following values:
9393

9494
#### `defaultStatus`
9595

96-
The default status of the drawer - whether the drawer should stay `open` or `closed` by default.
96+
The default status of the drawer - whether the drawer should stay `open` or `closed` by default.
9797

9898
When this is set to `open`, the drawer will be open from the initial render. It can be closed normally using gestures or programmatically. However, when going back, drawer will re-open if it was closed. This is essentially the opposite of the default behavior of the drawer where it starts `closed`, and back button closes an open drawer.
9999

@@ -568,6 +568,39 @@ import { useDrawerStatus } from '@react-navigation/drawer';
568568
const isDrawerOpen = useDrawerStatus() === 'open';
569569
```
570570

571+
If you can't use the hook, you can also use the `getDrawerStatusFromState` helper:
572+
573+
```js
574+
import { getDrawerStatusFromState } from '@react-navigation/drawer';
575+
576+
// ...
577+
578+
const isDrawerOpen = getDrawerStatusFromState(navigation.getState()) === 'open';
579+
```
580+
581+
For class components, you can listen tp the `state` event to check if drawer was opened or closed:
582+
583+
```js
584+
class Profile extends React.Component {
585+
componentDidMount() {
586+
this._unsubscribe = navigation.addListener('state', () => {
587+
const isDrawerOpen =
588+
getDrawerStatusFromState(navigation.getState()) === 'open';
589+
590+
// do something
591+
});
592+
}
593+
594+
componentWillUnmount() {
595+
this._unsubscribe();
596+
}
597+
598+
render() {
599+
// Content of the component
600+
}
601+
}
602+
```
603+
571604
## Nesting drawer navigators inside others
572605

573606
If a drawer navigator is nested inside of another navigator that provides some UI, for example a tab navigator or stack navigator, then the drawer will be rendered below the UI from those navigators. The drawer will appear below the tab bar and below the header of the stack. You will need to make the drawer navigator the parent of any navigator where the drawer should be rendered on top of its UI.

0 commit comments

Comments
 (0)