You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 3, 2022. It is now read-only.
🏄♀️ 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!
5
+
🏄♀️ Surfing the wave of React Hook hype with a few convenience hooks for `@react-navigation/core` v3/v4. Destined to work on web, server, and React Native. Contributions welcome!
6
+
7
+
**IMPORTANT**: [react-navigation v5](https://github.com/react-navigation/navigation-ex) is already on its way and is a full rewrite (including hooks). This project will not live past v4, and will try to make the migration path from v4 to v5 easy by not introducing any new hook that won't be in v5.
5
8
6
9
## Examples (web only so far)
7
10
@@ -95,13 +98,80 @@ function ReportNavigationEvents() {
95
98
96
99
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
97
100
101
+
### useIsFocused()
102
+
103
+
Convenient way to know if the screen currently has focus.
Permit to execute an effect when the screen takes focus, and cleanup the effect when the screen loses focus.
115
+
116
+
```js
117
+
functionMyScreen() {
118
+
useFocusEffect(useCallback(() => {
119
+
console.debug("screen takes focus");
120
+
return () =>console.debug("screen loses focus");
121
+
}, []));
122
+
return<View>...</View>;
123
+
}
124
+
```
125
+
126
+
**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.
127
+
128
+
`useFocusEffect` can be helpful to refetch some screen data on params changes:
129
+
130
+
```js
131
+
functionProfile({ userId }) {
132
+
const [user, setUser] =React.useState(null);
133
+
134
+
constfetchUser=React.useCallback(() => {
135
+
constrequest=API.fetchUser(userId).then(
136
+
data=>setUser(data),
137
+
error=>alert(error.message)
138
+
);
139
+
140
+
return () =>request.abort();
141
+
}, [userId]);
142
+
143
+
useFocusEffect(fetchUser);
144
+
145
+
return<ProfileContent user={user} />;
146
+
}
147
+
```
148
+
149
+
150
+
`useFocusEffect` can be helpful to handle hardware back behavior on currently focused screen:
0 commit comments