React Native ScrollView Adding a picture array

Source: Internet
Author: User

"Use Strict"

Import React, {Component} from ' React ';
Import {
Appregistry,//Register component, is the application of JS run the portal
StyleSheet,//style sheet, similar to a collection containing properties of individual components
ScrollView,
Dimensions,//specifications
Image,//Picture component
View//Views Components
} from ' react-native ';

Size of the current device screen
const {width, height} = dimensions.get (' window ')

Image
var IMAGES = [
Require ('./resource/test/1.png '),
Require ('./resource/test/2.jpeg '),
Require ('./resource/test/3.png '),
Require ('./resource/test/1.png '),
Require ('./resource/test/2.jpeg '),
Require ('./resource/test/3.png '),
Require ('./resource/test/1.png '),
Require ('./resource/test/2.jpeg '),
Require ('./resource/test/3.png ')

];

Declaring a Helloworld component
Class HelloWorld extends Component {


/* Reference: http://www.cnblogs.com/huanglifen/p/5175082.html
* Please note this function, the returned image must have a key
* If there is no key, the warning will be reported?? ------Each child in an array or iterator should has a unique "key" props (see note)
* Use the map function in ScrollView to create loops.
* */
renderimages (key, i) {
        Key:1, 2, 3, 4, 5 ...
I: 0, 1, 2, 3, 4 ...
Return <image Key={i} style={{flex:1, width, height}} Source={images[key]} resizemode={' contain '}/>
    }

Render () {//Render

Return (

/*
* Set space height:
-1: Set the height directly to the ScrollView
-2: Settings related to the parent control of the ScrollView height < note: The parent component needs to be added style={{flex:1}>
The -3:scrollview itself cannot be set flex:1

The following code slides horizontally, if the vertical direction is flexdirection: ' column ' + horizontal={false}
* */
<scrollview contentcontainerstyle={{flexdirection: ' Row '}}
Showshorizontalscrollindicator={true}
Horizontal={true}
Pagingenabled={true}
>
{images.map (key, i) = = This.renderimages (key, i)}
</ScrollView>
);
}
}
Appregistry.registercomponent (' HelloWorld ', () = HelloWorld);


Note:
1. Reference: http://www.cnblogs.com/huanglifen/p/5175082.html + http://www.zhihu.com/question/37701739
2. Set key description : Key is necessary when traversing or looping the output to render sub-components

****************************************  ****************************************

React Native ScrollView Adding a picture array

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.