I am learning react native and in all the tutorials i see ListView has been used with only 1 items per row. I have not used ListView, though. I have only 6 items that has to be shown as flat grid with 2 items per row and should be responsive. I know its a basic question, but i have tried from my side too which can be seen in the image
This is my code
renderDeviceEventList() { return _.map(this.props.deviceEventOptions, deviceEventOption => ( <View key={deviceEventOption.id}> <Icon name={deviceEventOption.icon_name} color="#ddd" size={30} onPress={() => this.props.selectDeviceEvent(deviceEventOption) } /> <Text style={{ color: "#ff4c4c" }}> {deviceEventOption.icon_name} </Text> </View> )); } render() { return ( <View style={{ flex: 1, top: 60, flexDirection: "row", justifyContent: "space-around", flexWrap: "wrap", marginBottom: 10 }} > {this.renderDeviceEventList()} </View> ); }
To show 2 items per row with React Native, we can set flexDirection to 'row' and flexWrap to 'wrap' . to set flexDirection and flexWrap so we get a horizontal flex layout and we wrap overflowing components to the next row. Then we set each View 's width to 50% so that they take half the width of the screen.
You want to do something like this: import React, { Component } from "react"; import { Text, View, StyleSheet, TextInput } from "react-native"; export default class App extends Component { render() { return ( <View style={styles. row}> <View style={styles.
To add space between two elements on the same line in React Native, we can set justifyContent to 'space-between' in the View . to set the flexDirection to 'row' and the justifyContent style to 'space-between' to spread the Text components on the screen.
React Practice Course. We will import List in our Home component and show it on screen. To create a list, we will use the map() method. This will iterate over an array of items, and render each one. When we run the app, we will see the list of names.
The correct way to do it would be with flexBasis
, with a value set to (1/n)%
where n
is the desired # of rows > 0. For two rows:
.parent { flex: 1; flexWrap: 'wrap'; flexDirecton: 'row'; } .child { flexBasis: '50%'; }
You can try the flat list from react native. where in you can specific the number of columns and also can mention the vertical direction or horizontal direction. Sample code:
<FlatList data={this.props.data} keyExtractor={this._keyExtractor} //has to be unique renderItem={this._renderItem} //method to render the data in the way you want using styling u need horizontal={false} numColumns={2} />
Refer https://facebook.github.io/react-native/docs/flatlist.html for more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With