Skip to content
This repository was archived by the owner on Dec 3, 2022. It is now read-only.

Commit 18ced88

Browse files
committed
docs
1 parent ab60ff2 commit 18ced88

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

README.md

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# React Navigation Hooks
2+
23
[![npm version](https://badge.fury.io/js/react-navigation-hooks.svg)](https://badge.fury.io/js/react-navigation-hooks) [![npm downloads](https://img.shields.io/npm/dm/react-navigation-hooks.svg)](https://www.npmjs.com/package/react-navigation-hooks) [![CircleCI badge](https://circleci.com/gh/react-navigation/hooks/tree/master.svg?style=shield)](https://circleci.com/gh/react-navigation/hooks/tree/master) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://reactnavigation.org/docs/contributing.html)
34

45
🏄‍♀️ 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!
@@ -95,13 +96,80 @@ function ReportNavigationEvents() {
9596

9697
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
9798

99+
### useIsFocused()
100+
101+
Convenient way to know if the screen currently has focus.
102+
103+
```js
104+
function MyScreen() {
105+
const isFocused = useIsFocused();
106+
return <Text>{isFocused ? 'Focused' : 'Not Focused'}</Text>;
107+
}
108+
```
109+
110+
### useFocusEffect(callback)
111+
112+
Permit to execute an effect when the screen takes focus, and cleanup the effect when the screen loses focus.
113+
114+
```js
115+
function MyScreen() {
116+
useFocusEffect(useCallback(() => {
117+
console.debug("screen takes focus");
118+
return () => console.debug("screen loses focus");
119+
}));
120+
return <View>...</View>;
121+
}
122+
```
123+
124+
**NOTE**: To avoid the running the effect too often, it's important to wrap the callback in useCallback before passing it to `useFocusEffect` as shown in the example. The effect will re-execute everytime the callback changes if the screen is focused.
125+
126+
`useFocusEffect` can be helpful to refetch some screen data on params changes:
127+
128+
```js
129+
function Profile({ userId }) {
130+
const [user, setUser] = React.useState(null);
131+
132+
const fetchUser = React.useCallback(() => {
133+
const request = API.fetchUser(userId).then(
134+
data => setUser(data),
135+
error => alert(error.message)
136+
);
137+
138+
return () => request.abort();
139+
}, [userId]);
140+
141+
useFocusEffect(fetchUser);
142+
143+
return <ProfileContent user={user} />;
144+
}
145+
```
146+
147+
148+
`useFocusEffect` can be helpful to handle hardware back behavior on currently focused screen:
149+
150+
```js
151+
const useBackHandler = (backHandler: () => boolean) => {
152+
useFocusEffect(() => {
153+
const subscription = BackHandler.addEventListener('hardwareBackPress', backHandler);
154+
return () => subscription.remove();
155+
});
156+
};
157+
```
158+
159+
160+
161+
98162
### useFocusState()
99163

164+
**deprecated**: this hook does not exist in v5, you should rather use `useIsFocused`
165+
166+
100167
Convenient way of subscribing to events and observing focus state of the current screen.
101168

102169
```js
103-
function MyFocusTag() {
104-
return <p>{useFocusState().isFocused ? 'Focused' : 'Not Focused'}</p>;
170+
function MyScreen() {
171+
const focusState = useFocusState();
172+
return <Text>{focusState.isFocused ? 'Focused' : 'Not Focused'}</Text>;
105173
}
106174
```
107175

@@ -111,3 +179,4 @@ One (always, and only one) of the following values will be true in the focus sta
111179
- isBlurring
112180
- isBlurred
113181
- isFocusing
182+

0 commit comments

Comments
 (0)