|
| 1 | +# React Navigation Hooks |
| 2 | + |
| 3 | +[](https://badge.fury.io/js/react-navigation-hooks) [](https://circleci.com/gh/react-navigation/react-navigation-hooks/tree/master) [](https://reactnavigation.org/docs/contributing.html) |
| 4 | + |
| 5 | +## Docs |
| 6 | + |
| 7 | +🏄♀️ Surfing the wave of React Hook hype with a few convenience hooks for `@react-navigation/core` v3. Destined to work on web, server, and React Native. Contributions welcome! |
| 8 | + |
| 9 | +### useNavigation |
| 10 | + |
| 11 | +This is the main convenience hook. It provides the regular navigation prop, as you'd get via the screen prop or by using `withNavigation`. |
| 12 | + |
| 13 | +You can use the navigate functionality anywhere in your app: |
| 14 | + |
| 15 | +```js |
| 16 | +function MyLinkButton() { |
| 17 | + // be careful to never call useNavigation in the press callback. Call hooks directly from the render function! |
| 18 | + const { navigate } = useNavigation(); |
| 19 | + return ( |
| 20 | + <Button |
| 21 | + onPress={() => { |
| 22 | + navigate('Home'); |
| 23 | + }} |
| 24 | + title="Go Home" |
| 25 | + /> |
| 26 | + ); |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### useNavigationParam |
| 31 | + |
| 32 | +Access a param for the current navigation state |
| 33 | + |
| 34 | +```js |
| 35 | +function MyScreen() { |
| 36 | + const { routeName } = useNavigationState(); |
| 37 | + return <p>My route name is {routeName}</p>; |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +Literally the same as `useNavigation().getParam(paramName)` |
| 42 | + |
| 43 | +### useNavigationState |
| 44 | + |
| 45 | +Access the navigation state of the current route, or if you're in a navigator view, access the navigation state of your sub-tree. |
| 46 | + |
| 47 | +```js |
| 48 | +function MyScreen() { |
| 49 | + const { routeName } = useNavigationState(); |
| 50 | + return <p>My route name is {routeName}</p>; |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +Literally the same as `useNavigation().state` |
| 55 | + |
| 56 | +### useNavigationKey |
| 57 | + |
| 58 | +Convenient way to access the key of the current route. |
| 59 | + |
| 60 | +Literally the same as `useNavigationState().key` |
| 61 | + |
| 62 | +### useNavigationEvents |
| 63 | + |
| 64 | +Subscribe to navigation events in the current route context. |
| 65 | + |
| 66 | +```js |
| 67 | +function ReportNavigationEvents() { |
| 68 | + const [events, setEvents] = useState([]); |
| 69 | + useNavigationEvents(evt => { |
| 70 | + // latest state on evt.state |
| 71 | + // prev state on evt.lastState |
| 72 | + // triggering navigation action on evt.action |
| 73 | + |
| 74 | + setEvents(events => [...events, evt.type]); |
| 75 | + // evt.type is [will/did][Focus/Blur] |
| 76 | + }); |
| 77 | + return ( |
| 78 | + <> |
| 79 | + {events.map(evt => ( |
| 80 | + <p>{evt.type}</p> |
| 81 | + ))} |
| 82 | + </> |
| 83 | + ); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +The event payload will be the same as provided by `addListener`, as documented here: https://reactnavigation.org/docs/en/navigation-prop.html#addlistener-subscribe-to-updates-to-navigation-lifecycle |
| 88 | + |
| 89 | +### useFocusState |
| 90 | + |
| 91 | +Convenient way of subscribing to events and observing focus state of the current screen. |
| 92 | + |
| 93 | +```js |
| 94 | +function MyFocusTag() { |
| 95 | + return <p>{useFocusState().isFocused ? 'Focused' : 'Not Focused'}</p>; |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +One of the following values will always be true in the focus state: |
| 100 | + |
| 101 | +- isFocused |
| 102 | +- isBlurring |
| 103 | +- isBlurred |
| 104 | +- isFocusing |
0 commit comments