非常教程

React native参考手册

开始 | Getting Started

State

有两种类型的数据控制组件:propsstateprops由父级设置,它们在组件的整个生命周期中都是固定的。对于将要改变的数据,我们必须使用state

通常,您应该state在构造函数中进行初始化,然后setState在想要更改时调用它。

例如,假设我们想让文字始终闪烁。文本本身在闪烁组件被创建时被设置一次,所以文本本身就是一个prop。“文本当前是打开还是关闭”随着时间的推移而变化,所以应该保持在state

import React, { Component } from 'react';
import { AppRegistry, Text, View } from 'react-native';

class Blink extends Component {
  constructor(props) {
    super(props);
    this.state = {showText: true};

    // Toggle the state every second
    setInterval(() => {
      this.setState(previousState => {
        return { showText: !previousState.showText };
      });
    }, 1000);
  }

  render() {
    let display = this.state.showText ? this.props.text : ' ';
    return (
      <Text>{display}</Text>
    );
  }
}

export default class BlinkApp extends Component {
  render() {
    return (
      <View>
        <Blink text='I love to blink' />
        <Blink text='Yes blinking is so great' />
        <Blink text='Why did they ever take this out of HTML' />
        <Blink text='Look at me look at me look at me' />
      </View>
    );
  }
}

// skip this line if using Create React Native App
AppRegistry.registerComponent('AwesomeProject', () => BlinkApp);

在实际应用中,你可能不会用定时器设置状态。当有新的数据从服务器到达时,或者从用户输入时,您可能会设置状态。您也可以使用像Redux这样的状态容器来控制数据流。在这种情况下,您将使用Redux来修改您的状态,而不是setState直接调用。

当调用setState时,BlinkApp将重新渲染其组件。通过调用Timer中的setState,该组件将在每次定时器滴答时重新呈现。

State的工作方式与在React中的工作方式相同,因此有关处理状态的更多详细信息,可以查看React.Component API。在这一点上,你可能会很烦恼,到目前为止,我们的大多数例子都使用了无聊的默认黑色文本。为了让事情更美好,你必须了解Style。

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