onNetworkStatusChange(listener: Function)

Requires:
BaseKit≥2.0.1@ray-js/ray≥0.3.23
Platforms:
iOSAndroid
import { onNetworkStatusChange } from '@ray-js/ray';

// Promise usage
try {
  const res = await onNetworkStatusChange({ ...params });
  console.log(res);
} catch (err) {
  console.log(err.errorMsg);
}

Description

Listen for network status changes. Note: On Android, this event only notifies that the network state changed. To get the latest network status, call the getNetworkType API again. Note: On Android, when the network state changes, the system may not update the network status immediately; there can be a delay. Therefore, when this event is triggered, you may need to call getNetworkType with a delay to obtain the latest information.

Parameters

ParameterTypeRequiredDescription
Yes

Return Value

None

Examples

import { onNetworkStatusChange } from '@ray-js/ray'

// Listen for network status changes; when switching networks (toggle Wi-Fi / Airplane mode), the callback returns the connection state and network type
onNetworkStatusChange(e => {
  console.log("Network changed:", e.isConnected, e.networkType);
});
// Changes are triggered by switching networks; keep the listener active for a while
await new Promise(resolve => setTimeout(resolve, 8000));