Android React Native custom Component Process
Assume that we have a requirement to customize a component, which consists of a small icon and a text description of the graph, with a background color, a background color configurable, and a width and height configurable. As shown in, there are two such components.
First, create a js file under the index. android. js directory and name it item. js. Enter the template code in it. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KCgoKPHByZSBjbGFzcz0 = "brush: java;"> /*** Sample React Native App* https://github.com/facebook/react-native*/'use strict';var React = require('react-native');var { StyleSheet, Text, View, Image,} = React;var Item = React.createClass({ render: function() { return ( ); },});var styles = StyleSheet.create({});module.exports = Item;
Note the last sentenceModule. exports = Item;To export components.
The peripheral component of our system should be a View. An Image and Text are nested in the View, and the View, Image, and Text have corresponding styles. You can also set the Image address, the text content can be set, while the View's background color, height, width, and other styles should be passed in externally and work together with the style inside the component. This can be implemented using the style array, that isStyle = {[style1, style2, style3]}Finally, the information returned by the render function is as follows.
{text}
" data-snippet-id="ext.20c155944f536ddefceb3ad985596b1f" data-snippet-saved="false" data-codota-status="done">
{text}
Now we get the attribute value set externally.
var outStyle={ width:parseInt(this.props.width), height:parseInt(this.props.height), backgroundColor:this.props.color,}var img=this.props.img;var text=this.props.text;
So the final code is like this.
{text}
); },});" data-snippet-id="ext.a1ee96691ceb318e34e967fc4eb0fed1" data-snippet-saved="false" data-csrftoken="vpkyUR1T-z6G3oFFcR3BjCFx21rhhslN4uMw" data-codota-status="done">var Item = React.createClass({ render: function() { var outStyle={ width:parseInt(this.props.width), height:parseInt(this.props.height), backgroundColor:this.props.color, } var img=this.props.img; var text=this.props.text; return (
{text}
); },});
Inside, we need to style it.
var styles = StyleSheet.create({ item:{ justifyContent:'center', alignItems:'center', borderRadius:10, marginLeft:5, marginRight:5, marginTop:5, marginBottom:5, }, image:{ width:48, height:48, }, text:{ color:'#ffffff', alignItems:'center' }});
Of course, this View can also extract many attributes as custom attributes. This example only extracts several attributes for demonstration.
How to use it?
It is also very easy. Introduce this component into the file to be used, use the defined tag, and set the corresponding attribute.
Introduce Components
var Item = require('./item');
Use Components
); },});var styles = StyleSheet.create({ container:{ flexDirection:'row', justifyContent:'center', alignItems:'center', backgroundColor:'#ff0000' }, item1:{ marginLeft:100, }, item2:{ marginLeft:100, }});" data-snippet-id="ext.766e4568c42c583094d28ff6f898d1dd" data-snippet-saved="false" data-csrftoken="25KQm5HJ-O8bFyH36J_KWjyc14AV8sNyHAN8" data-codota-status="done">var AwesomeProject = React.createClass({ render: function() { return (
); },});var styles = StyleSheet.create({ container:{ flexDirection:'row', justifyContent:'center', alignItems:'center', backgroundColor:'#ff0000' }, item1:{ marginLeft:100, }, item2:{ marginLeft:100, }});
We can see that we usedItemTag, and set custom attributes in the tag. These attributes will be set to our component.