React Native--组件Component
创建一个Component一个组件类可以像前面Hello World工程中那样通过 class HelloWorldAppp extends Component 来创建,或者通过React.createClass来创建,并且提供一个render方法以及其他可选的生命周期函数、组件相关的事件或方法定义。 因此,HelloWorldAppp和下面的实现方法是等价的: var HelloWorldAppp = React.createClass({
render() {
return (
<View
</View>
);
},});
通过继承Component实现的组件中如果实现getDefaultProps getInitialState等方法时,会有下面警告: Warning: getDefaultProps was defined on HelloWorldAppp,a plain JavaScript class. This is only supported for classes created using React.createClass. Use a static property to define defaultProps instead.
组件生命周期先来看一段代码: import React,{ Component } from 'react';
import {
AppRegistry,StyleSheet,Text,View,Image,TouchableHighlight
} from 'react-native';
var clickTime = 0;
var HelloWorldAppp = React.createClass({
getDefaultProps(){
console.log("getDefaultProps")
return {title:"HelloWorld"}
},getInitialState(){
console.log("getInitialState")
return {content:"点击屏幕任意位置"}
},componentWillMount(){
console.log("componentWillMount")
},componentDidMount(){
console.log("componentDidMount")
},shouldComponentUpdate(nextProps,nextState){
console.log("shouldComponentUpdate")
return true
},componentWillUpdate(nextProps,nextState){
console.log("componentWillUpdate")
},componentDidUpdate(prevProps,prevState){
console.log("componentDidUpdate")
},render() {
console.log("render")
return (
<TouchableHighlight
onPress={() => this.backgorundClicked()}
underlayColor = '#ddd'
style = {styles.container}
>
<Text style={styles.welcome}>{clickTime > 0 ? this.state.content : this.props.title + " n " + this.state.content}</Text>
</TouchableHighlight>
);
},backgorundClicked(){
clickTime++
this.setState({
content:"第"+clickTime+"次点击"
});
}
});
AppRegistry.registerComponent('AwesomeProject',() => HelloWorldAppp); const styles = StyleSheet.create({ container: { flex: 1,justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF',},welcome: { fontSize: 20,textAlign: 'center',margin: 10,title_text:{ fontSize:18,} });
然后认识一下组件中的一些函数:
this.setState({ content:"第"+clickTime+"次点击" });
值得注意的是,一旦调用了this.setState方法,控件必将调用render方法,对控件进行再次的渲染,不过,React框架会自动根据DOM的状态来判断是否需要真正的渲染。
生命周期函数
上面函数的调用顺序是:
总得来讲,React Native组件的生命周期,经历了Mount->Update->Unmount这三个大的过程,即从创建到销毁的过程,如果借助Android和iOS的开发思想,那么React Native组件的生命周期就更容易理解了。那么,我们构建一个React Native控件也就能够知道如何下手,如何控制和优化。经过一层一层的封装和调用,一个完整的React Native应用也就构建出来了。 Props(属性)和 State(状态)Props 就是组件的属性,由外部通过 JSX 属性传入设置,一旦初始设置完成,就可以认为 this.props 是不可更改的,所以不要轻易更改设置 this.props 里面的值(虽然对于一个 JS 对象你可以做任何事)。 class MyText extends Component{
render() {
return(
<Text style={this.props.style}>{this.props.content}</Text>
);
}
}
class HelloWorldAppp extends Component{
render() {
return (
<MyText style={styles.welcome} content="HelloWorld"/>
);
}
}
State 是组件的当前状态,可以把组件简单看成一个“状态机”,根据状态 state 呈现不同的 UI 展示。一旦状态(数据)更改,组件就会自动调用 render 重新渲染 UI,这个更改的动作会通过 this.setState 方法来触发。 class HelloWorldAppp extends Component{
constructor(props) {
super(props);
this.state = {showText:"Test"};
}
render() {
return (
<View>
<Text style={styles.title_text}>{this.state.showText}</Text>
</View>
);
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |