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

 

簡要分析:

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

 

今天到此為止,下次見。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM