react native api

react native 开发过程中遇到过并且用过的一些api,做个汇总方便了解。特别是下面一些没有设置连接的部分,有兴趣的自行到中英文官网中查找如何与原生交互。不是没有连接表示不重要,相反的,是因为特别重要,而本人技术和表达能力有限不容易解释清楚,抱歉。另外有兴趣的请多了解18、19部分。

1. AppRegistry

This API only works in projects made with react-native init or in those made with Create React Native App which have since ejected. For more information about ejecting, please see the guide on the Create React Native App repository.

AppRegistry is the JS entry point to running all React Native apps. App root components should register themselves with AppRegistry.registerComponent, then the native system can load the bundle for the app and then actually run the app when it’s ready by invoking AppRegistry.runApplication.

To “stop” an application when a view should be destroyed, call AppRegistry.unmountApplicationComponentAtRootTag with the tag that was passed into runApplication. These should always be used as a pair.

AppRegistry should be required early in the require sequence to make sure the JS execution environment is setup before other modules are required.

AppRegistry是JS运行所有React Native应用的入口。应用的根组件应当通过AppRegistry.registerComponent方法注册自己,然后原生系统才可以加载应用的代码包并且在启动完成之后通过调用AppRegistry.runApplication来真正运行应用。

要“结束”一个应用并销毁视图的话,请调用AppRegistry.unmountApplicationComponentAtRootTag方法,参数为在runApplication中使用的标签名。它们必须严格匹配。

AppRegistry应当在require序列中尽可能早的被require到,以确保JS运行环境在其它模块之前被准备好。

import {AppRegistry} from 'react-native';
import React, {Component} from 'react';
import {Provider} from 'react-redux';
import configureStore from './app/redux/Store';
import App from './app/containers/App';

//获取store
const store = configureStore();

export default class Root extends Component {
    render() {
        return (
            //store覆盖整改项目
            <Provider store={store}>
                <App />
            </Provider>
        )
    }
}

AppRegistry.registerComponent('XXXX', () => Root);

2. AppState

AppState can tell you if the app is in the foreground or background, and notify you when the state changes.

AppState is frequently used to determine the intent and proper behavior when handling push notifications.

App States
active - The app is running in the foreground
background - The app is running in the background. The user is either in another app or on the home screen
inactive - This is a state that occurs when transitioning between foreground & background, and during periods of inactivity such as entering the Multitasking view or in the event of an incoming call

AppState(此组件能用于Android)能告诉你应用当前是在前台还是在后台,并且能在状态变化的时候通知你。

AppState通常在处理推送通知的时候用来决定内容和对应的行为。

App States
active - 应用正在前台运行
background - 应用正在后台运行。用户既可能在别的应用中,也可能在桌面。
inactive - 这是一个过渡状态,不会在正常的React Native应用中出现。

3. AsyncStorage

AsyncStorage is a simple, unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of LocalStorage.

It is recommended that you use an abstraction on top of AsyncStorage instead of AsyncStorage directly for anything more than light usage since it operates globally.

On iOS, AsyncStorage is backed by native code that stores small values in a serialized dictionary and larger values in separate files. On Android, AsyncStorage will use either RocksDB or SQLite based on what is available.

The AsyncStorage JavaScript code is a simple facade that provides a clear JavaScript API, real Error objects, and simple non-multi functions. Each method in the API returns a Promise object.

AsyncStorage是一个简单的、异步的、持久化的Key-Value存储系统,它对于App来说是全局性的。它用来代替LocalStorage。

我们推荐您在AsyncStorage的基础上做一层抽象封装,而不是直接使用AsyncStorage。

译注:推荐由React Native中文网封装维护的react-native-storage模块,提供了较多便利功能。

本模块的JS代码提供了对原生实现的一个封装,以提供一个更清晰的JS API、返回真正的错误对象,以及简单的单项对象操作函数。每个方法都会返回一个Promise对象。

4. BackHandler

Detect hardware button presses for back navigation.

Android: Detect hardware back button presses, and programmatically invoke the default back button functionality to exit the app if there are no listeners or if none of the listeners return true.

tvOS: Detect presses of the menu button on the TV remote. (Still to be implemented: programmatically disable menu button handling functionality to exit the app if there are no listeners or if none of the listeners return true.)

iOS: Not applicable.

The event subscriptions are called in reverse order (i.e. last registered subscription first), and if one subscription returns true then subscriptions registered earlier will not be called.

监听设备上的后退按钮事件。

Android:监听后退按钮事件。如果没有添加任何监听函数,或者所有的监听函数都返回false,则会执行默认行为,退出应用。

tvOS(即Apple TV机顶盒):监听遥控器上的后退按钮事件(阻止应用退出的功能尚未实现)。

iOS:尚无作用。

监听函数是按倒序的顺序执行(即后添加的函数先执行)。如果某一个函数返回true,则后续的函数都不会被调用。

5. DeviceInfo

6. Dimensions

例子:var {height, width} = Dimensions.get('window');

7. NativeEventEmitter

8. NetInfo

NetInfo exposes info about online/offline status

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 describes the type of connection the device is using to communicate with the network.

Cross platform values for ConnectionType:

none - device is offline
wifi - device is online and connected via wifi, or is the iOS simulator
cellular - device is connected via Edge, 3G, WiMax, or LTE
unknown - error case and the network status is unknown
Android-only values for ConnectionType:

bluetooth - device is connected via Bluetooth
ethernet - device is connected via Ethernet
wimax - device is connected via WiMAX
EffectiveConnectionType enum
Cross platform values for EffectiveConnectionType:

2g
3g
4g
unknown
Android
To request network info, you need to add the following line to your app’s AndroidManifest.xml:

isConnectionExpensive
Available on Android. Detect if the current active connection is metered or not. A network is classified as metered when the user is sensitive to heavy data usage on that connection due to monetary costs, data limitations or battery/performance issues.

NetInfo模块可以获知设备联网或离线的状态信息。

NetInfo.fetch().done((reach) => {
console.log(‘Initial: ’ + reach);
});
function handleFirstConnectivityChange(reach) {
console.log(‘First change: ’ + reach);
NetInfo.removeEventListener(
‘change’,
handleFirstConnectivityChange
);
}
NetInfo.addEventListener(
‘change’,
handleFirstConnectivityChange
);
IOS
以异步的方式判断设备是否联网,以及是否使用了移动数据网络。

none - 设备处于离线状态。
wifi - 设备处于联网状态且通过wifi链接,或者是一个iOS的模拟器。
cell - 设备是通过Edge、3G、WiMax或是LTE网络联网的。
unknown - 发生错误,网络状况不可知
Android
请求网络信息需要先在应用的AndroidManifest.xml文件中添加如下权限字段:


Android的联网类型:

NONE - 设备处于离线状态
BLUETOOTH - 蓝牙数据连接
DUMMY - 模拟数据连接
ETHERNET - 以太网数据连接
MOBILE - 移动网络数据连接
MOBILE_DUN - 拨号移动网络数据连接
MOBILE_HIPRI - 高优先级移动网络数据连接
MOBILE_MMS - 彩信移动网络数据连接
MOBILE_SUPL - 安全用户面定位(SUPL)数据连接
VPN - 虚拟网络连接。需要Android5.0以上
WIFI - WIFI数据连接
WIMAX - WiMAX数据连接
UNKNOWN - 未知数据连接
其他的连接状态已被Android API隐藏,但可以在需要时使用。

isConnectionExpensive
此方法仅Android可用。用于判断当前活动的连接是否计费。如果当前连接是通过移动数据网络,或者通过基于移动数据网络所创建的wifi热点,都有可能被判定为计费的数据连接。

9. PermissionsAndroid

PermissionsAndroid provides access to Android M’s new permissions model. Some permissions are granted by default when the application is installed so long as they appear in AndroidManifest.xml. However, “dangerous” permissions require a dialog prompt. You should use this module for those permissions.

On devices before SDK version 23, the permissions are automatically granted if they appear in the manifest, so check and request should always be true.

If a user has previously turned off a permission that you prompt for, the OS will advise your app to show a rationale for needing the permission. The optional rationale argument will show a dialog prompt only if necessary - otherwise the normal permission prompt will appear.

PermissionsAndroid可以访问Android M(也就是6.0)开始提供的权限模型。有一些权限写在AndroidManifest.xml就可以在安装时自动获得。但有一些“危险”的权限则需要弹出提示框供用户选择。本API即用于后一种情形。

在低于Android 6.0的设备上,权限只要写在AndroidManifest.xml里就会自动获得,此情形下check和request 方法将始终返回true。

如果用户之前拒绝过你的某项权限请求,那么系统会建议你显示一个为什么需要这个权限的“详细解释”(rationale参数)。如果用户之前拒绝过,那么当你再次申请的时候,弹出的就可能不是原先的申请信息,而是rationale参数里提供的进一步解释。

10. NativeEventEmitter

11. PixelRatio

PixelRatio class gives access to the device pixel density.

PixelRatio类提供了访问设备的像素密度的方法。

12. Share

13. DeviceEventEmitter

14. NativeAppEventEmitter

15. NativeModules

16. I18nManager

17. Keyboard

18. 原生模块

19. NativeMethodsMixin

NativeMethodsMixin提供了一些用于直接访问底层原生组件的方法。这在你需要聚焦(focus)一个视图或者计算它在屏幕上显示的尺寸之类的情况下可能会需要。

这些方法在大部分React Native提供的默认的组件中都可以使用。注意,它们不能在一些复合组件(并非直接由原生视图构成)中使用,这可能包括你自己在应用中定义的绝大部分组件。想了解更多信息,可以参阅直接操作
英文文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值