《React-Native系列》30、 RN组件间通信

来源:互联网 发布:ktv伴奏软件 编辑:程序博客网 时间:2024/04/28 04:09

今天我们来说一说RN的组件之间的通信。

ReactNative的核心之一是他的组件化,组件化的核心是组件之间的通信。

组件是有层级来区分的,譬如:父组件 子组件。


我们先来讲解一个例子。


这个是我们要实现的功能,是一个表单的一部分,首先我们想到的是抽象组件。

组件有2种状态

  • 选中状态,显示后面的课时
  • 未选中状态,不显示后面的课时

组件的代码如下:

[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import React, { Component } from 'react';  
  2. var {  
  3.   StyleSheet,  
  4.   View,  
  5.   Text,  
  6.   Image,  
  7.   TextInput,  
  8.   PixelRatio,  
  9.   TouchableHighlight,  
  10.   Dimensions,  
  11.   TextInput,  
  12.   TouchableWithoutFeedback,  
  13.   TouchableOpacity,  
  14. } = require('react-native')  
  15.   
  16. const {screenWidth, screenHeight} = Dimensions.get('window');  
  17. const PxRatio = 2;  
  18.   
  19. export default class CourseType extends Component{  
  20.   
  21.     constructor(props) {  
  22.       super(props);  
  23.       this.state={  
  24.         choosed : this.props.choosed,  
  25.       }  
  26.     }  
  27.   
  28.     modifyChoosed(choosed){  
  29.       this.setState({choosed : choosed});  
  30.     }  
  31.   
  32.   render() {  
  33.     return (  
  34.       <View style={styles.row}>  
  35.         <TouchableOpacity style={styles.imageView} onPress={this.props.onPress} >  
  36.           <Image source={this.state.choosed == 0 ?require('image!unselected') :require('image!red_selected') }   />  
  37.         </TouchableOpacity>  
  38.         <View style={styles.textInputTitle}>  
  39.           <Text  
  40.             style={styles.textTitle}>  
  41.             {this.props.title}  
  42.           </Text>  
  43.         </View>  
  44.         <Text style={{flex:1}}/>  
  45.         {  
  46.           this.state.choosed == 0 ? null :  
  47.           <TextInput  
  48.             style={styles.ksValueView}  
  49.             maxLength={5}  
  50.             placeholder="0"  
  51.             placeholderTextColor="#b2b2b2"  
  52.             multiline={false}  
  53.             onChangeText={()=>{}}  
  54.             keyboardType="numeric"  
  55.             />  
  56.         }  
  57.         {  
  58.           this.state.choosed == 0 ? null :  
  59.             <View style={styles.ksTipView} >  
  60.               <Text style={styles.ksTipText}>元/课时</Text>  
  61.             </View>  
  62.         }  
  63.       </View>  
  64.     );  
  65.   }  
  66. }  
  67.   
  68. const styles = StyleSheet.create({  
  69.     row: {  
  70.       backgroundColor: '#ffffff',  
  71.       flexDirection: 'row',  
  72.       height: 90 / PxRatio,  
  73.       alignItems: 'center',  
  74.       width:screenWidth,  
  75.     },  
  76.     imageView: {  
  77.       marginLeft: 30/PxRatio,  
  78.     },  
  79.   
  80.     textInputTitle: {  
  81.       marginLeft:20/PxRatio,  
  82.       alignItems:'center',  
  83.     },  
  84.     textTitle:{  
  85.       fontSize: 28/PxRatio,  
  86.       color:'#666666',  
  87.     },  
  88.   
  89.     ksValueView:{  
  90.       width:128/PxRatio,  
  91.       height:52/PxRatio,  
  92.       alignSelf:'center',  
  93.       borderWidth: 1/PxRatio,  
  94.       borderColor:'#dbdbdb',  
  95.       textAlign:'center'  
  96.     },  
  97.   
  98.     ksTipView:{  
  99.       marginRight:30/PxRatio,  
  100.       marginLeft:20/PxRatio,  
  101.     },  
  102.     ksTipText:{  
  103.       fontSize: 28/PxRatio,  
  104.       color:'#333333',  
  105.     },  
  106.   });  

抽象出来的组件有一个状态机变量choosed,来控制是否有被选中,他的值是由外部props传入。

提供了一个onPress方法,控制选中状态的切换。其中这个方法是由props传递过来的。

定义了modifyChoosed方法来修改状态机变量choosed


好,组件封装完毕,那在表单页面我们怎么来使用这个组件呢。

1、import组件模块

[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import CourseType from './CourseType';  

2、使用组件

[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <CourseType ref="stu" title="学生上门" choosed={this.state.type_stu} onPress={()=>{  
  2.                   let choosed = this.state.type_stu == 0 ? 1:0;  
  3.                   this.refs.stu.modifyChoosed(choosed);  
  4.                   this.setState({type_stu:choosed})  
  5.                 }} />  

这里说明下

  • 定义了CourseType组件的一个ref属性,ref="stu"
  • 定义了title属性,
  • 定义了choosed属性,他的值是由表单的type_stu状态机变量控制
  • 定义了onPress方法,他的实现是:先获取choosed状态(取反),通过this.refs.stu.调用CourseType组件的modifyChoosed方法修改choosed状态,修改表单的type_stu状态机变量

好了,这样我们就实现了功能。


那我们总结下,这个是重点。

表单相当于父组件,CourseType相当于子组件。


子组件可以通过this.props 调用父组件的方法。


那父组件如何调用子组件的方法呢?
首先在子组件上定义一个ref=xxx,然后在父组件内通过this.refs.xxx.子组件Method()来调用。


0 0