Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/framework/react/react-native.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Comment on lines +44 to +53
Copy link

Copilot AI Feb 26, 2026

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.

Suggested change
let initialised = false
const eventSubscription = Network.addNetworkStateListener((state) => {
initialised = true
setOnline(!!state.isConnected)
})
Network.getNetworkStateAsync()
.then((state) => {
if (!initialised) {
let initialized = false
const eventSubscription = Network.addNetworkStateListener((state) => {
initialized = true
setOnline(!!state.isConnected)
})
Network.getNetworkStateAsync()
.then((state) => {
if (!initialized) {

Copilot uses AI. Check for mistakes.
setOnline(!!state.isConnected)
}
})
.catch(() => {
// getNetworkStateAsync can reject on some platforms/SDK versions
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If getNetworkStateAsync() rejects (which the comment acknowledges can happen on some platforms/SDK versions) and the listener hasn't fired yet, the online state will never be initialized. The onlineManager will default to "online" (true), which could be incorrect if the device is actually offline. Consider adding logic in the catch block to handle this scenario, such as assuming offline as a safer default or attempting an alternative method to determine initial connectivity.

Suggested change
// 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 uses AI. Check for mistakes.
})

return eventSubscription.remove
Copy link

Copilot AI Feb 26, 2026

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.

Suggested change
return eventSubscription.remove
return () => eventSubscription.remove()

Copilot uses AI. Check for mistakes.
})
```
Expand Down