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!
@@ -95,13 +96,80 @@ function ReportNavigationEvents() {
95
96
96
97
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
98
99
+
### useIsFocused()
100
+
101
+
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.
113
+
114
+
```js
115
+
functionMyScreen() {
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
+
functionProfile({ userId }) {
130
+
const [user, setUser] =React.useState(null);
131
+
132
+
constfetchUser=React.useCallback(() => {
133
+
constrequest=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:
0 commit comments