Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ReScript bindings for @react-native-community/netinfo

License

NotificationsYou must be signed in to change notification settings

rescript-react-native/netinfo

Build StatusVersionReScript Forum

ReScript bindings for@react-native-community/netinfo.

Exposed asReactNativeNetInfo module.

@rescript-react-native/netinfo X.y.* means it's compatible with@react-native-community/netinfo X.y.*

Installation

When@react-native-community/netinfois properly installed & configured by following their installation instructions,you can install the bindings:

npm install @rescript-react-native/netinfo#oryarn add @rescript-react-native/netinfo

@rescript-react-native/netinfo should be added tobs-dependencies in yourbsconfig.json:

{  //...  "bs-dependencies": [    "@rescript/react",    "rescript-react-native",    // ...+    "@rescript-react-native/netinfo"  ],  //...}

Usage

Types

netInfoConfiguration

To be used only when the platform does not natively supply information oninternet reachability.

PropertyTypeDescription
reachabilityUrlstringURL used to test if the internet is reachable.
reachabilityTestresponse => boolA function to handle theresponse object returned when the reachability URL is called. It should returntrue if the response indicates that the internet is reachable.
reachabilityShortTimeoutfloatNumber of seconds between internet reachability checks when the internet was not previously detected.
reachabilityLongTimeoutfloatNumber of seconds between internet reachability checks when the internet was previously detected.
reachabilityRequestTimeoutfloatNumber of milliseconds allowed before reachability check fails.

netInfoStateType

Kind of the current network connection. Valid values are:

ValuePlatformsConnection State
noneAndroid, iOS, WindowsNot active
unknownAndroid, iOS, WindowsUndetermined
cellularAndroid, iOS, WindowsActive
wifiAndroid, iOS, WindowsActive
bluetoothAndroidActive
ethernetAndroid, WindowsActive
wimaxAndroidActive
vpnAndroidActive
otherAndroid, iOS, WindowsActive

netInfoCellularGeneration

Cellular generation of the current network connection. Valid values are:

ValueNotes
net2gInlined as "2g". Returned for CDMA, EDGE, GPRS and IDEN connections
net3gInlined as "3g". Returned for EHRPD, EVDO, HSPA, HSUPA, HSDPA and UTMS connections.
net4gInlined as "4g". Returned for HSPAP and LTE connections

netInfoState

typenetInfoState= {  ."_type":netInfoStateType,"isConnected":bool,"isInternetReachable":bool,"isWifiEnabled":bool,"details":Js.Null.t(details),};
  • isConnected will betrue if there is an active connection (but not implythat the internet is necessarily reachable).
  • isInternetReachable will betrue if the internet can be reached using theactive connection
  • isWifiEnabled will betrue if WiFi is enabled on the device, andfalseotherwise.Android only.

details key will have valueJs.Null.empty (null) when_type isnull orunknown.

details

details depends on_type given withinnetInfoState. If_type is notnull orunknown,details is an object as below:

typedetails= {  ."isConnectionExpensive":bool,"ssid":Js.Nullable.t(string),"strength":Js.Nullable.t(int),"ipAddress":Js.Nullable.t(string),"subnet":Js.Nullable.t(string),"cellularGeneration":Js.Nullable.t(netInfoCellularGeneration),"carrier":Js.Nullable.t(string),};
PropertyPlatformTypeDescription
isConnectionExpensiveAndroid, iOS, WindowsboolIf network connection is consideredexpensive in either energy or monetary terms.

Note that some keys may only exist in the JS object when_type iswifi orcellular. Accordingly, in Reason, keys may have valuesJs.Nullable.undefined.

  • ssid,strength,ipAddress andsubnet will have valueJs.Nullable.undefined unless_type iswifi.
  • cellularGeneration andcarrier will have valueJs.Nullable.undefinedunless_type is cellular.
_type iswifi
PropertyPlatformTypeDescription
ssidAndroid, iOS (not tvOS)Js.Nullable.t(string)SSID of the network. May have valueJs.Nullable.undefined,Js.Nullable.null, or be an empty string if undetermined.On iOS, make sure your app meets at least one of thefollowing requirements. On Android, make sure theACCESS_FINE_LOCATION permission is listed inAndroidManifest.xml and accepted by the user.
strengthAndroidJs.Nullable.t(string)If signal strength can be determined, will be an integer number from0 to5. May have valueJs.Nullable.undefined otherwise.
ipAddressAndroid, iOSJs.Nullable.t(string)External IP address. Can be in IPv4 or IPv6 format. May have valueJs.Nullable.undefined if undetermined.
subnetAndroid, iOSJs.Nullable.t(string)The subnet mask in IPv4 format. May have valueJs.Nullable.undefined if undetermined.
type iscellular
PropertyPlatformTypeDescription
cellularGenerationAndroid, iOS, WindowsNetInfoCellularGenerationGeneration of cell network the user is connected to. This can give an indication of speed, but no guarantees. May have valueJs.Nullable.null if generation cannot be determined.
carrierAndroid, iOSstringThe network carrier name. May have valueJs.Nullable.undefined or may be empty if undetermined.

Methods

configure

configure:netInfoConfiguration=>unit

fetch

To query state of the active connection, returnsnetInfoState wrapped in aPromise.

fetch:unit=>Js.Promise.t(netInfoState)

Below example demonstrates determination of the cellular connection generation,using this method.

React.useEffect0(()=> {Js.Promise.(ReactNativeNetInfo.fetch()|>then_(w=>         {switch (w##details->Js.Null.toOption) {           |None=>"Connection type is none or unknown"->Js.Console.warn           |Some(x)=>lety=x##cellularGeneration;switch (y->Js.Nullable.toOption) {             |None=>if (y==Js.Nullable.undefined) {"Connection type is wifi, bluetooth, ethernet, wimax, vpn or other"->Js.Console.warn;               }else {"Connection generation unknown"->Js.Console.warn;               }             |Some(z)=>if (z==ReactNativeNetInfo.net2G) {"2G connection"->Js.Console.warn;               }elseif (z==ReactNativeNetInfo.net3G) {"3G connection"->Js.Console.warn;               }else {"4G connection"->Js.Console.warn;               }             };           };         }->resolve       )|>catch(err=>"error"->Js.Console.warn->resolve)|>ignore  );None;});

fetchInterface

To query the connection state for a particular interface.

fetchInterface: [ |`cellular | `ethernet |`wifi] => Js.Promise.t(netInfoState)

addEventListener

To subscribe to the connection state; accepts a listener of typenetInfoState => unit and returns an unsubscribe method of typeunit => unit.The listener will be called once following subscription and each time connectionstate changes.

addEventListener: (netInfoState=>unit)=>t

where

typet=unit=>unit

Below example demonstrates subscribing to changes in connection state:

React.useEffect0(()=> {letremove=ReactNativeNetInfo.addEventListener(w=>      (switch (w##details->Js.Null.toOption) {        |None=>"Connection type is none or unknown"        |Some(x)=>lety=x##cellularGeneration;switch (y->Js.Nullable.toOption) {          |None=>if (y==Js.Nullable.undefined) {"Connection type is wifi, bluetooth, ethernet, wimax, vpn or other";            }else {"Connection generation unknown";            }          |Some(z)=>if (z==ReactNativeNetInfo.net2G) {"2G connection";            }elseif (z==ReactNativeNetInfo.net3G) {"3G connection";            }else {"4G connection";            }          };        }      )->Js.Console.warn    );Js.Console.warn(remove);Some(()=>remove());});

useNetInfo

This method returns a React Hook with typenetInfoState

useNetInfo:unit=>netInfoState

Below example demonstrates its use within aText component:

<Text>  (switch (ReactNativeNetInfo.useNetInfo()##details->Js.Null.toOption) {    |None=>"Connection type is none or unknown"    |Some(x)=>lety=x##cellularGeneration;switch (y->Js.Nullable.toOption) {      |None=>if (y==Js.Nullable.undefined) {"Connection type is wifi, bluetooth, ethernet, wimax, vpn or other";        }else {"Connection generation unknown";        }      |Some(z)=>if (z==ReactNativeNetInfo.net2G) {"2G connection";        }elseif (z==ReactNativeNetInfo.net3G) {"3G connection";        }else {"4G connection";        }      };    }  )->React.string</Text>

Changelog

Check thechangelog for more informations about recentreleases.


Contribute

Read thecontribution guidelinesbefore contributing.

Code of Conduct

We want this community to be friendly and respectful to each other. Please readour full code of conductso that you can understand what actions will and will not be tolerated.

About

ReScript bindings for @react-native-community/netinfo

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Contributors68


[8]ページ先頭

©2009-2025 Movatter.jp