React Native 快速入門之認識Props和State

眼下React Native(之後簡稱RN)愈來愈火,我也要投入到學習當中。對於一個前端來講,仍是有些難度。由於本人以爲這是一個App開發的領域,天然是不一樣。編寫本文的時候,RN的版本爲0.21.0。咱們立刻以代碼進入今天的學習。前端

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

class Hello extends Component {
  render() {
    return (
      <View>
          <Text>{ this.props.title}</Text>
          <Text>{ this.props.text}</Text>
      </View>
    );
  }
}
class HelloComponent extends React.Component{
    constructor (props) {
      super(props);
      this.state = {
         appendText: ''
      };
    }
    _setText() {
this.setState({appendText: ' Native!'}); } render() { return ( <View> <Text onPress={this._setText.bind(this)}> {this.props.text + this.state.appendText} </Text> </View> ); } } class learn01 extends Component { render() { const pros = { text: 'hi', title: 'title' } return ( <View> <View style={{height:30}} /> <Hello {...pros} /> <HelloComponent text="React"/> </View> ); } }

 

簡要分析:react

  1. 所謂props,就是屬性傳遞,並且是單向的。
  2. 屬性多的時候,能夠傳遞一個對象,語法爲{...xx},這是es6的新特性。
  3. React靠一個state來維護狀態,當state發生變化則更新DOM。

 

今天到此爲止,下次見。es6

相關文章
相關標籤/搜索