轉載連接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/react-native-foundation-course/react
React-native是Facebook的開源項目,它的口號是"Learning once,write anywhere",目的是統一的View的編寫。react-native
1、React-Native基本語法模板緩存
'use strict'; =====>(嚴格模式)服務器
var React = require('react-native'); =====>(導入模塊react-native,關鍵字是: require)組件化
var {flex
AppRegistry,ui
StyleSheet, =====>(聲明要用到得系統組件)this
Text,spa
View,component
} = 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; =====>(導出組件,使可以在別的組件中用)
2、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>
)
}
})
3、React-Native生命週期
a、getInitialState: 在組建被掛載以前調用,咱們通常在裏面定義初始state值
b、getDefaultProps: 在組件類建立的時候調用一次,而後返回值被緩存下來。若是父組件沒有指定 getDefaultProps 中定義的屬性,則此處返回的對象中的相應屬性將會合併到 this.props
c、componentWillMount: 服務器端和客戶端都只調用一次,在初始化渲染執行以前馬上調用
d、render: 執行視圖的渲染操做
e、componentDidMount: 在初始化渲染執行以後馬上調用一次,僅客戶端有效(服務器端不會調用)
f、componentWillUnmount: 組件從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;