"React-native Series" 30, RN components communication between

Source: Internet
Author: User
Tags abstract require


Today, let's talk about the communication between components of Rn.



One of the core of Reactnative is his modular, component-centric communication between components.



Components are differentiated by hierarchy, such as parent component subcomponents.






Let's start with an example.






This is what we want to achieve, it's part of a form, and the first thing we think about is an abstract component.



There are 2 states of the component



Checked status, showing the following session is unchecked, not showing the following hours



The code for the component is as follows:





import React, { Component } from 'react';
var {
  StyleSheet,
  View,
  Text,
  Image,
  TextInput,
  PixelRatio,
  TouchableHighlight,
  Dimensions,
  TextInput,
  TouchableWithoutFeedback,
  TouchableOpacity,
} = require('react-native')

const {screenWidth, screenHeight} = Dimensions.get('window');
const PxRatio = 2;

export default class CourseType extends Component{

    constructor(props) {
      super(props);
      this.state={
        choosed : this.props.choosed,
      }
    }

    modifyChoosed(choosed){
      this.setState({choosed : choosed});
    }

  render() {
    return (
      <View style={styles.row}>
        <TouchableOpacity style={styles.imageView} onPress={this.props.onPress} >
          <Image source={this.state.choosed == 0 ?require('image!unselected') :require('image!red_selected') }   />
        </TouchableOpacity>
        <View style={styles.textInputTitle}>
          <Text
            style={styles.textTitle}>
            {this.props.title}
          </Text>
        </View>
        <Text style={{flex:1}}/>
        {
          this.state.choosed == 0 ? null :
          <TextInput
            style={styles.ksValueView}
            maxLength={5}
            placeholder="0"
            placeholderTextColor="#b2b2b2"
            multiline={false}
            onChangeText={()=>{}}
            keyboardType="numeric"
            />
        }
        {
          this.state.choosed == 0 ? null :
            <View style={styles.ksTipView} >
              <Text style={styles.ksTipText}>元/课时</Text>
            </View>
        }
      </View>
    );
  }
}

const styles = StyleSheet.create({
    row: {
      backgroundColor: '#ffffff',
      flexDirection: 'row',
      height: 90 / PxRatio,
      alignItems: 'center',
      width:screenWidth,
    },
    imageView: {
      marginLeft: 30/PxRatio,
    },

    textInputTitle: {
      marginLeft:20/PxRatio,
      alignItems:'center',
    },
    textTitle:{
      fontSize: 28/PxRatio,
      color:'#666666',
    },

    ksValueView:{
      width:128/PxRatio,
      height:52/PxRatio,
      alignSelf:'center',
      borderWidth: 1/PxRatio,
      borderColor:'#dbdbdb',
      textAlign:'center'
    },

    ksTipView:{
      marginRight:30/PxRatio,
      marginLeft:20/PxRatio,
    },
    ksTipText:{
      fontSize: 28/PxRatio,
      color:'#333333',
    },
  });

The abstract component has a state machine variable choosed, to control whether there is a selection, his value is passed in by an external props.


Provides a onpress method that controls the toggle of the selected state. One of these methods is passed by props.



Defines the modifychoosed method to modify the state machine variable choosed






Well, the components are encapsulated, so how do we use this component on the form page?



1. Import Component Module





Import Coursetype from './coursetype ';


2. Using components





<coursetype ref= "Stu" title= "Student Door" Choosed={this.state.type_stu} onpress={() =>{let
                  choosed = This.state.type_stu = = 0? 1:0;
                  This.refs.stu.modifyChoosed (choosed);
                  This.setstate ({type_stu:choosed})
                }}/>

Here's the explanation.


Defines a ref property for the Coursetype component, ref= "Stu" defines the title property, defines the choosed attribute, whose value is defined by the TYPE_STU state machine variable control of the form Onpress method, His implementation is: first obtain the choosed state (inversion), through the this.refs.stu. Call the Coursetype component's Modifychoosed method to modify the choosed state, modify the Type_stu state machine variable of the form



OK, so that we can achieve the function.






Well, let's conclude, this is the point.



A form is equivalent to a parent component, and Coursetype is equivalent to a subassembly.






Subcomponents can call the parent component's methods through This.props.




How does the parent component invoke the child component's method?
You first define a ref=xxx on the subassembly and then invoke it within the parent component through the This.refs.xxx. Subcomponent method ().





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.