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
feat(react-query): allow useQuery and useQueries to unsubscribe from the query cache with an option (#8348)
* feat(react-query): allow useQuery and useQueries to unsubscribe from the query cache with an option
* test: subscribed
* fix: revert calling getOptimisticResult later
* docs(react): update the react-native.md section (#8506)
* update the doc
* update the doc
---------
Co-authored-by: Dominik Dorfmeister <[email protected]>
* docs: reference
---------
Co-authored-by: Florian De la comble <[email protected]>
In the above code, `refetch` is skipped the first time because `useFocusEffect` calls our callback on mount in addition to screen focus.
94
94
95
-
## Disable re-renders on out of focus Screens
96
-
97
-
In some situations, including performance concerns, you may want to stop re-renders when a React Native screen gets out of focus. To achieve this we can use `useFocusEffect` from `@react-navigation/native` together with the `notifyOnChangeProps` query option.
98
-
99
-
This custom hook provides a `notifyOnChangeProps` option that will return an empty array whenever a screen goes out of focus - effectively stopping any re-renders on that scenario. Whenever the screens gets in focus again, the behavior goes back to normal.
In the above code, `useFocusEffect` is used to change the value of a reference that the callback will use as a condition.
95
+
## Disable queries on out of focus screens
136
96
137
-
The argument is wrapped in a reference to also guarantee that the returned callback always keeps the same reference.
97
+
If you don’t want certain queries to remain “live” while a screen is out of focus, you can use the subscribed prop on useQuery. This prop lets you control whether a query stays subscribed to updates. Combined with React Navigation’s useIsFocused, it allows you to seamlessly unsubscribe from queries when a screen isn’t in focus:
Enabled can also be set to a callback to support disabling queries on out of focus screens without state and re-rendering on navigation, similar to how notifyOnChangeProps works but in addition it wont trigger refetching when invalidating queries with refetchType active.
When subscribed is false, the query unsubscribes from updates and won’t trigger re-renders or fetch new data for that screen. Once it becomes true again (e.g., when the screen regains focus), the query re-subscribes and stays up to date.
Copy file name to clipboardExpand all lines: docs/framework/react/reference/useQuery.md
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,6 +53,7 @@ const {
53
53
select,
54
54
staleTime,
55
55
structuralSharing,
56
+
subscribed,
56
57
throwOnError,
57
58
},
58
59
queryClient,
@@ -161,6 +162,10 @@ const {
161
162
- Defaults to `true`
162
163
- If set to `false`, structural sharing between query results will be disabled.
163
164
- If set to a function, the old and new data values will be passed through this function, which should combine them into resolved data for the query. This way, you can retain references from the old data to improve performance even when that data contains non-serializable values.
165
+
-`subscribed: boolean`
166
+
- Optional
167
+
- Defaults to `true`
168
+
- If set to `false`, this instance of `useQuery` will not be subscribed to the cache. This means it won't trigger the `queryFn` on its own, and it won't receive updates if data gets into cache by other means.
0 commit comments