转载连接: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;