Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show 2 items per row[react native]

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

enter image description here

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>     ); } 
like image 340
Serenity Avatar asked Nov 04 '16 08:11

Serenity


People also ask

How do you display items in a row in react?

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.

How do you set two inputs on the same row in React Native?

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.

How do you show two text in the same line in React Native?

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.

How do I display list of items in React Native?

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.


2 Answers

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%'; } 
like image 83
nikk wong Avatar answered Sep 18 '22 12:09

nikk wong


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.

like image 37
Thanmai C Avatar answered Sep 18 '22 12:09

Thanmai C