react-native基礎教程(1)

轉載鏈接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/react-native-foundation-course/


React-native是Facebook的開源項目,它的口號是"Learning once,write anywhere",目的是統一的View的編寫。這些是入門的基礎。

一、React-Native基本語法模板

'use strict';  =====>(嚴格模式)

var React = require('react-native');   =====>(導入模塊react-native,關鍵字是: require)

var {

  AppRegistry,

  StyleSheet,     =====>(聲明要用到得系統組件)

  Text,

  View,

} = React;

var FirstApp = React.createClass({   =====>(創建組件名稱是:FirstApp, 關鍵字是createClass)

  render: function() {   =====>(渲染方法, 組件中必須得方法)

return (

 

              <View style={styles.container}>=====>(這幾行就是JSX語法寫的)

                                              <Text style={{fontSize: 18}}>這是我的第一個React Native APP</Text>   =====>(顯示在手機屏幕上的內容在這寫) 

 

              </View>=====>(這裏用view包起來,而不是用div)

          );

    }

});

 

var styles = StyleSheet.create( =====>(創建樣式,看上面加粗劃線的代碼,可以在這裏定義,也可以直接寫在代碼裏面,如上面的fontSize:18)

  container: {

      flex: 1,

      justifyContent: 'center',      

      alignItems: 'center',

      backgroundColor: 'orange'

    }

});

 

AppRegistry.registerComponent('FirstApp', () => FirstApp);   =====>(註冊應用,使能夠加載運行, FirstApp就是你的App名稱)

 

module.exports = FirstApp; =====>(導出組件,使能夠在別的組件中用)

二、React-native的 組件化,我們可以把你需要的分功能自定義米快寫代碼,然後把所有的模塊組合起開,就是一個完整的程序(這樣子寫代碼看起比較清晰)

代碼如下所示:

'use strict';

var React=require('react-native');

var {

AppRegistry,

StyleSheet,

Text,

View

}=React;

var FirstApp=React.createClass({

render:function(){

<View style={styles.container}>  

<HelloWorld myText='我是第一'/>

<HelloWorld myText='我是第二'/>==>(這裏引用了下一個組件,HelloWorld自動成爲FiirstApp的子組件)

<HelloWorld myText='我是第三'/>

</View>

 

}

 

});

var HelloWorld=React.createClass({

render:function(){

return (

<View>

<Text style={{fontSize:20,color:'red'}}>{this.props.myText}</Text>

=====>(從父組件傳過來的myText屬性,用this.props.myText接收) 

</View>

)

 

}

 

 

})

 

三、React-Native生命週期

agetInitialState: 在組建被掛載之前調用,我們一般在裏面定義初始state

   bgetDefaultProps: 在組件類創建的時候調用一次,然後返回值被緩存下來。如果父組件沒有指定 getDefaultProps 中定義的屬性,則此處返回的對象中的相應屬性將會合併到 this.props

  ccomponentWillMount: 服務器端和客戶端都只調用一次,在初始化渲染執行之前立刻調用

  drender: 執行視圖的渲染操作

  ecomponentDidMount: 在初始化渲染執行之後立刻調用一次,僅客戶端有效(服務器端不會調用)

     fcomponentWillUnmount: 組件從DOM中移除時調用,一般在次方法進行必要的清理工作

組件的執行順序示例:

'use strict';

var React = require('react-native');

var {

  AppRegistry,

  StyleSheet,

  Text,

  View,

} = React;

var FirstApp = React.createClass({

    getDefaultProps: function() {

console.log('getDefaultProps');

},

getInitialState: function() {

console.log('getInitialState')

  return { };

 },

componentWillMount: function() {

 console.log('componentWillMount');

    },

componentDidMount: function() {

console.log('componentDidMount');

    },

componentWillUnmount: function() {

console.log('componentWillUnmount');

    },

ender: function() {

 console.log('render');

 return (

 

              <View style={styles.container}>

 

                  <HelloWorld myText='我是第一' />

                  <HelloWorld myText='我是第二' />

                  <HelloWorld myText='我是第三' />

 

              </View>

          );

    }

});

 

var HelloWorld = React.createClass({

  

    render: function() {

 

        return (

 

              <View>

                  <Text style={{fontSize: 20, color: 'red'}}>{this.props.myText}</Text>

              </View>

          );

    }  

});

 

var styles = StyleSheet.create({

  container: {

      flex: 1,

      justifyContent: 'center',

      alignItems: 'center',

      backgroundColor: 'orange'

    }

});

 

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

 

module.exports = FirstApp;

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章