ReactNative官网例子练习——(一)

来源:互联网 发布:ps基线标准和已优化 编辑:程序博客网 时间:2024/06/11 11:56

    学一个变成语言,一般我们都先完成一个hellowrold。这就不写了,然后根据Rn官网的顺序 我们需要了解 Props(属性)和State(状态)

        Props 属性用于定制一些必要的参数

        State 可以动态的改变一些状态 一般在构造方法中初始化 constructor中。

小例子:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /**  
  2.  * Sample React Native App  
  3.  * https://github.com/facebook/react-native  
  4.  * @flow  
  5.  */  
  6.   
  7. import React, { Component } from 'react';  
  8. import {  
  9.   AppRegistry,  
  10.   StyleSheet,  
  11.   Text,  
  12.   View,  
  13.   Image  
  14. } from 'react-native';  
  15.   
  16. class HaiSheng extends Component {  
  17.   render() {  
  18.      let pic = {  
  19.       uri : 'https://upload.wikimedia.org/wikipedia/commons/d/de/Bananavarieties.jpg'  
  20.     };  
  21.     return (  
  22.       <View style={styles.container}>  
  23.         
  24.         <Text style={styles.welcome}>  
  25.           Welcome to React Native!  
  26.         </Text>  
  27.         <Text style={styles.namec}> 欢迎大海!我是大海 你们好哇!</Text>  
  28.        <Image source={pic} style={{width: 200, height: 200}} />  
  29.         <Greeting name="Rxjava"/>  
  30.         <Greeting name="RxAndroid"/>  
  31.         <Greeting name="Rxxdux"/>  
  32.   
  33.         <Blink text='I love to blink' />  
  34.         <Blink text='Yes blinking is so great' />  
  35.         <Blink text='Why did they ever take this out of HTML' />  
  36.         <Blink text='Look at me look at me look at me' />  
  37.         <View style={{width: 50,height: 50,backgroundColor:"green"}}/>  
  38.       </View>  
  39.     );  
  40.   }  
  41. }  
  42. class Greeting extends Component{  
  43.   render(){  
  44.     return(  
  45.       <View style={{alignItems:"center"}}>  
  46.       <Text> Hello {this.props.name} </Text>  
  47.       </View>  
  48.     );  
  49.   }  
  50. }  
  51.   
  52. class Blink extends Component{  
  53.   constructor(props){  
  54.     super(props);  
  55.     this.state = {showText: true};  
  56.   // 每1000毫秒对showText状态做一次取反操作  
  57.   setInterval(() => {  
  58.       this.setState({ showText: !this.state.showText });  
  59.     }, 1000);  
  60.     
  61.   }  
  62.   render(){  
  63. // 根据当前showText的值决定是否显示text内容  
  64. let display = this.state.showText ? this.props.text : ' ';  
  65. return (  
  66.       <Text>{display}</Text>  
  67.     );  
  68.   }  
  69. }  
  70.   
  71. const styles = StyleSheet.create({  
  72.   container: {  
  73.     flex: 1,  
  74.     justifyContent: 'center',  
  75.     alignItems: 'center',  
  76.     backgroundColor: '#F5FCFF',  
  77.       
  78.   },  
  79.   welcome: {  
  80.     fontSize: 20,  
  81.     textAlign: 'center',  
  82.     margin: 10,  
  83.   },  
  84.   namec:{  
  85.     fontSize:26,  
  86.     textAlign:"center",  
  87.     margin:10,  
  88.   }  
  89.    
  90. });  
  91.   
  92. AppRegistry.registerComponent('HaiSheng', () => HaiSheng);  

显示效果:

0 0
原创粉丝点击