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.
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;
@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.
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