React Native Tutorial摘要(一)

###安装 在Mac上用homebrew安装很是简单:html

brew install node
brew install watchman
npm install -g react-native-cli

固然前提也须要安装好Xcode和Android SDK及虚拟机。node

而后就能够玩了,初始化一个projectreact

react-native init AwesomeProject

这个时间稍微有点长,完成后可启动ios

cd AwesomeProject
react-native run-ios

官方向导
固然在按照官方向导时,安装Android Studio后,记得要使用SDK Manager和AVD Manager安装目标版本的SDK和模拟器。git

###简单的用例 语法使用ES6标准,另外须要了解些React基本概念,如JSX、Component、state和props。github

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

class HelloWorldApp extends Component {
  render() {
    return (
      <Text>Hello world!</Text>
    );
  }
}

AppRegistry.registerComponent('HelloWorldApp', () => HelloWorldApp);

将上面代码贴入index.ios.js,而后在已经跑起来的模拟器中cmd+R就能够即时加载。npm

** 上面相似于在JS中写html的形式,在这里叫JSX。** ** 上面看起来像html的Text,它实际上是一个Component。**react-native

  • 要使用Component,须要先import。
  • AppRegistry只是为了告诉React Native谁是Root Component。

####Props数组

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

class Bananas extends Component {
  render() {
    let pic = {
      uri: 'https://upload.wikimedia.org/wikipedia/commons/d/de/Bananavarieties.jpg'
    };
    return (
      <Image source={pic} style={{width: 193, height: 110}}/>
    );
  }
}

AppRegistry.registerComponent('Bananas', () => Bananas);

以像html的attribute的形式注入组件中。this

在我看来,它是抽出一些特性出来支持定制,让Component能够容易复用。 如:

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

class Greeting extends Component {
  render() {
    return (
      <Text>Hello {this.props.name}!</Text>
    );
  }
}

class LotsOfGreetings extends Component {
  render() {
    return (
      <View style={{alignItems: 'center'}}>
        <Greeting name='Rexxar' />
        <Greeting name='Jaina' />
        <Greeting name='Valeera' />
      </View>
    );
  }
}

AppRegistry.registerComponent('LotsOfGreetings', () => LotsOfGreetings);

####state state也是Component内部带有一种模型,与props不一样,它的目的是为了支持值变化。

  • 在构造器中初始化它,而后当想change的时候调用setState从新设置。
  • 真实场景多是,当一条请求返回数据回调时,或者用户输入数据时等。
  • 官方有提到Redux,推荐使用Redux——一个state容器,去控制data flow。那样不会用太多机会直接调用setStats方法。
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({ showText: !this.state.showText });
    }, 1000);
  }

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

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>
    );
  }
}

AppRegistry.registerComponent('BlinkApp', () => BlinkApp);

State works the same way as it does in React, so for more details on handling state, you can look at the React.Component API.

####Style React组件中能够使用类CSS同样去控制样式,能够将background-color这样的使用backgroundColor去对照使用。 Style是一个命名为style的props,全部的Core component都支持它。

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

class LotsOfStyles extends Component {
  render() {
    return (
      <View>
        <Text style={styles.red}>just red</Text>
        <Text style={styles.bigblue}>just bigblue</Text>
        <Text style={[styles.bigblue, styles.red]}>bigblue, then red</Text>
        <Text style={[styles.red, styles.bigblue]}>red, then bigblue</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  bigblue: {
    color: 'blue',
    fontWeight: 'bold',
    fontSize: 30,
  },
  red: {
    color: 'red',
  },
});

AppRegistry.registerComponent('LotsOfStyles', () => LotsOfStyles);

**style能够接受数组,如_[styles.bigblue, styles.red]_。相同的样式,后面的将覆盖前面的。 **StyleSheet.create中,每个对象如一个class。

相关文章
相关标签/搜索