How to align view in react native in loop?

How to align view in react native in loop?



Here is my React Native component code.


import React, Component from 'react';
import View, Text,StyleSheet from 'react-native';

export default class AnatomyExample extends Component
render()
let data = [1,2,3,4,5,6,7,8];
return (
<View style=styles.main>
data.map(this.renderView)
</View>


);


renderView(d)
return (
<View style=styles.child>
<Text>d</Text>
</View>
)





const styles = StyleSheet.create(
main:
borderRadius: 4,
borderWidth: 1,
borderColor: '#d6d7da',
,
child:
borderWidth:1,
borderColor:'blue',
width:150,
height:150,


);



I want to make component output look like this image.
enter image description here



How to float View side by side. float:left; would work in html css but not sure how to make it in react native.


float:left;






React-Native uses flexbox so floating does not work - you have to add flex-wrap: wrap to your container and give the children a width e.g. 100px.

– davidbucka
Sep 9 '18 at 9:16






@davidbucka can you give me an example, please?

– Krishna Karki
Sep 9 '18 at 9:16




1 Answer
1



You can add the styles like this, if you want 4 elements in a row else you can specify custom width


import Dimensions from 'react-native';

const width, height = Dimensions.get('window')
main:
flexDirection: 'row',
flexWrap: 'wrap',
borderRadius: 4,
borderWidth: 1,
borderColor: '#d6d7da',
,
child:
margin: 5,
borderWidth:1,
borderColor:'blue',
width: width / 4 - 12, // ... DeviceWidth / 4 - (marginLeft + marginRight + borderLeft + borderRight)
height: 50,
backgroundColor: 'gray'







Thank you Pratish

– Krishna Karki
Sep 10 '18 at 12:15



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế