非常教程

React native参考手册

APIs

NetInfo

NetInfo公开有关在线/离线状态的信息

NetInfo.getConnectionInfo().then((connectionInfo) => {
  console.log('Initial, type: ' + connectionInfo.type + ', effectiveType: ' + connectionInfo.effectiveType);
});
function handleFirstConnectivityChange(connectionInfo) {
  console.log('First change, type: ' + connectionInfo.type + ', effectiveType: ' + connectionInfo.effectiveType);
  NetInfo.removeEventListener(
    'connectionChange',
    handleFirstConnectivityChange
  );
}
NetInfo.addEventListener(
  'connectionChange',
  handleFirstConnectivityChange
);

ConnectionType enum

ConnectionType 描述设备用于与网络进行通信的连接类型。

以下各项的跨平台值ConnectionType: - none- 设备处于脱机状态 - wifi- 设备处于联机状态并通过wifi连接,或者是iOS模拟器 - cellular- 设备通过Edge,3G,WiMax或LTE连接unknown- 错误情况和网络状态未知

仅适用于Android的值ConnectionType: - bluetooth- 设备通过蓝牙连接 - ethernet- 设备通过以太网连接 - wimax- 设备通过WiMAX连接

EffectiveConnectionType enum

Cross platform values for EffectiveConnectionType: - 2g - 3g - 4g - unknown

Android

要请求网络信息,您需要将以下行添加到您的应用AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

isConnectionExpensive

在Android上可用。检测当前活动连接是否被计量。由于货币成本,数据限制或电池/性能问题,当用户对该连接上的大量数据使用敏感时,网络被分类为计量。

NetInfo.isConnectionExpensive()
.then(isConnectionExpensive => {
  console.log('Connection is ' + (isConnectionExpensive ? 'Expensive' : 'Not Expensive'));
})
.catch(error => {
  console.error(error);
});

isConnected

适用于所有平台。异步获取布尔值以确定Internet连接。

NetInfo.isConnected.fetch().then(isConnected => {
  console.log('First, is ' + (isConnected ? 'online' : 'offline'));
});
function handleFirstConnectivityChange(isConnected) {
  console.log('Then, is ' + (isConnected ? 'online' : 'offline'));
  NetInfo.isConnected.removeEventListener(
    'change',
    handleFirstConnectivityChange
  );
}
NetInfo.isConnected.addEventListener(
  'change',
  handleFirstConnectivityChange
);

Connectivity Types (deprecated)

以下连接类型已弃用。它们被已弃用的API fetchchange事件使用。

iOS连接类型(不赞成使用): - none- 设备处于离线状态 - wifi- 设备在线并通过WiFi连接,或者是iOS模拟器 - cell- 设备通过Edge,3G,WiMax或LTE连接 - unknown- 错误情况和网络状态为未知

Android连接类型(不建议使用)。- NONE- 设备处于脱机状态 - BLUETOOTH- 蓝牙数据连接。- DUMMY- 虚拟数据连接。- ETHERNET- 以太网数据连接。- MOBILE- 移动数据连接。- MOBILE_DUN- DUN特定的移动数据连接。- MOBILE_HIPRI高优先级移动数据连接。- MOBILE_MMS- 特定于MMS的移动数据连接。- MOBILE_SUPL- SUPL特定的移动数据连接。- VPN使用一个或多个本地承载的虚拟网络。需要API Level 21 - WIFI- WIFI数据连接。- WIMAX- WiMAX数据连接。- UNKNOWN- 未知的数据连接。

其他连接类型被Android API隐藏,但可以根据需要使用。

方法

static addEventListener(eventName, handler)

添加一个事件处理程序。支持的事件:

  • connectionChange:当网络状态改变时触发。事件处理程序的参数是一个带有键的对象:
    • type:A ConnectionType(上面列出)
    • effectiveType:An EffectiveConnectionType(上面列出)
  • change:此事件已被弃用。connectionChange改为听。当网络状态改变时触发。事件处理程序的参数是上面列出的不提倡使用的连接类型之一。

static removeEventListener(eventName, handler)

删除网络状态更改的侦听器。

static fetch()

此功能已弃用。getConnectionInfo改为使用。返回使用上面列出的某个不推荐使用的连接类型解决的承诺。

static getConnectionInfo()

返回解析为将对象与一个承诺typeeffectiveType键,其值是一个ConnectionType与一个EffectiveConnectionType分别(如上所述)。

static isConnectionExpensive()

属性

isConnected: ObjectExpression

与上述方法相同的对象,但侦听器接收表示Internet连接的布尔值。如果您只对设备是否具有互联网连接感兴趣,请使用此功能。

React native

React Native 是一个 JavaScript 的框架,用来撰写实时的、可原生呈现 iOS 和 Android 的应用。

主页 https://facebook.github.io/react-native/
源码 https://github.com/facebook/react-native
发布版本 0.49

React native目录

1.开始 | Getting Started
2.指南 | Guides
3.APIs
4.组件:ActivityIndicator | Components: ActivityIndicator
5.组件:按钮 | Components: Button
6.组件:CheckBox | Components: CheckBox
7.组件:DatePickerIOS | Components: DatePickerIOS
8.组件:DrawerLayoutAndroid | Components: DrawerLayoutAndroid
9.组件:FlatList | Components: FlatList
10.组件:图像 | Components: Image
11.组件:KeyboardAvoidingView | Components: KeyboardAvoidingView
12.Components: ListView
13.Components: MaskedViewIOS
14.Components: Modal
15.Components: NavigatorIOS
16.Components: Picker
17.Components: PickerIOS
18.Components: ProgressBarAndroid
19.Components: ProgressViewIOS
20.Components: RefreshControl
21.Components: ScrollView
22.Components: SectionList
23.Components: SegmentedControlIOS
24.Components: Slider
25.Components: SnapshotViewIOS
26.Components: StatusBar
27.Components: Switch
28.Components: TabBarIOS
29.Components: TabBarIOS.Item
30.Components: Text
31.Components: TextInput
32.Components: ToolbarAndroid
33.Components: TouchableHighlight
34.Components: TouchableNativeFeedback
35.Components: TouchableOpacity
36.Components: TouchableWithoutFeedback
37.Components: View
38.Components: ViewPagerAndroid
39.Components: VirtualizedList
40.Components: WebView
41.创建 | Contributing
42.指南(Android) | Guides (Android)
43.指南(IOS) | Guides (iOS)
44.其他 | Miscellaneous