React components defined by Native (cross-file use)

Source: Internet
Author: User



haha ~ ~ ~ today is the introduction of custom components and then go to use this component, let this component pass between the various files haha below start!!!!



What we're going to create is a custom button that first creates a JS file named MyButton, with a touch of background, a function that triggers an event response,



The image resource, and the image size, are determined by the value passed. (The parameters passed in are determined)



Ok!! Below we need to add some native controls (components) to the Mybutton.js file.


import React, {
  AppRegistry,
  Component,
  Image,
  TouchableHighlight,
} from ‘react-native‘;


Then start creating our button Components!!! In general, we'll use a component called Touchablehighlight to package the contents.


class MyButton extends Component {
	render() {
		return (
		<TouchableHighlight   
                       underlayColor={this.props.bgColor}  
                       activeOpacity={0.5} 
                       onPress={this.props.onPress}  
                  >  
		    
			<Image 
                                source={require(‘./res/himi.png‘)} 
				style={ {
                                        width: this.props.imgWidth,
                                        height: this.props.imgHeight
                                 }} 
                         />			
                 </TouchableHighlight> 
		)
	}
}


Which: we should pay attention to this Dongdong ~ this.props Sister's ~ ~ What is this ghost???



In fact, this is not a ghost ~ Popular to say this is an instantiated object for example, I created an object called ' ghost ', then the ghost =this.props , so the property of the ghost is the This.props attribute (method + property)



So, my this.props.xxx and so on wait for the instantiation of "ghost to pass", and then to manipulate the other content .... ?!!



Of course :



Note: There are three possible values for the This.props.children:



A. If the current component has no child nodes, it is undefined;



B. If there is a child node, the data type is object;



C. If there are more than one child node, the data type is array. So be careful when dealing with This.props.children.



Ok! After the component is created, we can go to transform the created component into an outgoing component


Module.exports = MyButton;


Ok!! The code above indicates that you can get this component out of the way! ~~~



Note: You can centralize a number of components on one component and then pass them out



Grand finale:



We can use this component boldly, for example, we use this component in another file:


<MyButton   
            bgColor=‘#000‘
            onPress ={()=>{Alert.alert(‘Himi‘, ‘ MyBtton IS Click! ‘);}}
            imgWidth={100}
            imgHeight={100}
            >  
</MyButton>


It can be seen that many of the attribute parameters are passed to This.props, and then some operations are performed!!!!!



Note: The name of the property must be the same ~ Otherwise this property can not find the ancestors ~ ~






React components defined by Native (cross-file use)


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.