-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
docs(react-native): fix example to include initial state #10172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -41,9 +41,23 @@ import { onlineManager } from '@tanstack/react-query' | |||||||||||||
| import * as Network from 'expo-network' | ||||||||||||||
|
|
||||||||||||||
| onlineManager.setEventListener((setOnline) => { | ||||||||||||||
| let initialised = false | ||||||||||||||
|
|
||||||||||||||
| const eventSubscription = Network.addNetworkStateListener((state) => { | ||||||||||||||
| initialised = true | ||||||||||||||
| setOnline(!!state.isConnected) | ||||||||||||||
| }) | ||||||||||||||
|
|
||||||||||||||
| Network.getNetworkStateAsync() | ||||||||||||||
| .then((state) => { | ||||||||||||||
| if (!initialised) { | ||||||||||||||
| setOnline(!!state.isConnected) | ||||||||||||||
| } | ||||||||||||||
| }) | ||||||||||||||
| .catch(() => { | ||||||||||||||
| // getNetworkStateAsync can reject on some platforms/SDK versions | ||||||||||||||
|
||||||||||||||
| // getNetworkStateAsync can reject on some platforms/SDK versions | |
| // getNetworkStateAsync can reject on some platforms/SDK versions | |
| if (!initialised) { | |
| // Fall back to a safer default when initial state is unknown | |
| setOnline(false) | |
| } |
Copilot
AI
Feb 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning eventSubscription.remove directly may cause a this binding issue when the cleanup function is called. If remove is not bound to eventSubscription, calling it later will result in this being undefined. Consider wrapping it in an arrow function: return () => eventSubscription.remove() to ensure the correct context, similar to the pattern used in examples/react/react-native/src/hooks/useAppState.ts:9.
| return eventSubscription.remove | |
| return () => eventSubscription.remove() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable name uses British spelling "initialised" while the codebase consistently uses American spelling "initialized" (see packages/angular-query-experimental/src/inject-is-fetching.ts:42, packages/react-query-next-experimental/src/HydrationStreamProvider.tsx:159). Consider changing to "initialized" for consistency with the rest of the codebase.