非常教程

React native参考手册

指南 | Guides

Timers

定时器是应用程序的重要组成部分,React Native实现浏览器定时器。

计时器

  • setTimeout,clearTimeout
  • setInterval, clearInterval
  • setImmediate, clearImmediate
  • requestAnimationFrame, cancelAnimationFrame

requestAnimationFrame(fn)不一样setTimeout(fn, 0)- 前者会在所有帧被冲洗后触发,而后者将尽快触发(iPhone 5S每秒超过1000倍)。

setImmediate在当前JavaScript执行块的末尾执行,在将批处理响应发送回本机之前执行。请注意,如果你setImmediatesetImmediate回调中调用,它将立即执行,它不会退回到原始状态。

Promise实现使用setImmediate它的异步性原语。

interactionManager

为什么构建好的本地应用程序感觉如此流畅的原因之一是避免了在交互和动画过程中的昂贵操作。在React Native中,我们目前有一个限制,即只有一个JS执行线程,但您可以使用它InteractionManager来确保长时间运行的工作计划在任何交互/动画完成后启动。

应用程序可以安排任务在与以下内容交互之后运行:

InteractionManager.runAfterInteractions(() => {
   // ...long-running synchronous task...
});

将此与其他调度方案进行比较:

  • requestAnimationFrame():用于随时间变化动画视图的代码。
  • setImmediate / setTimeout / setInterval():稍后运行代码,注意这可能会延迟动画。
  • runAfterInteractions():稍后运行代码,而不延迟活动动画。

触摸处理系统将一个或多个活动触摸视为“互动”,并会延迟runAfterInteractions()回调,直到所有触摸结束或取消。

InteractionManager还允许应用程序通过在动画开始时创建交互“句柄”来注册动画,并在完成时清除它:

var handle = InteractionManager.createInteractionHandle();
// run animation... (`runAfterInteractions` tasks are queued)
// later, on animation completion:
InteractionManager.clearInteractionHandle(handle);
// queued tasks run if all handles were cleared

TimerMixin

我们发现使用React Native创建的应用程序中致命的主要原因是由于定时器在组件卸载后触发。为了解决这个反复出现的问题,我们介绍了TimerMixin。如果你有TimerMixin,那么你可以更换您的通话setTimeout(fn, 500)this.setTimeout(fn, 500)(只是在前面加上this.),一切都将被妥善清理你当组件卸除。

该库不附带React Native - 为了在您的项目中使用它,您需要npm i react-timer-mixin --save从项目目录中安装它。

import TimerMixin from 'react-timer-mixin';

var Component = createReactClass({
  mixins: [TimerMixin],
  componentDidMount: function() {
    this.setTimeout(
      () => { console.log('I do not leak!'); },
      500
    );
  }
});

这将消除很多艰难的工作来追踪错误,例如组件卸载后超时引发的崩溃。

请记住,如果您为React组件使用ES6类,则不存在用于mixin的内置API。要使用TimerMixinES6类,我们推荐react-mixin。

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