Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive grid in React Native & Flex

I'm want to create the simplest two column grid ListView

item | item
item | item

I saw this post about creating the grid and that worked - ListView grid in React Native

I couldn't understand how to make my grid responsive? How can I set the size of the item to 50%?

I know it is a new language but I wish it could have some more detailed guides / documents.

like image 246
Gal Weiss Avatar asked May 30 '15 18:05

Gal Weiss


People also ask

Can you use grid in react native?

A grid layout can be defined as a layout in which components are arranged in form of rows and columns thus providing an easy way of allocating components on user interface without use of positioning and floating values, in order to use react native grid layout we need to include dependency of react native grid.

What is a responsive grid?

A responsive grid allows a layout to change dynamically based on the size of the screen. This also guarantees consistent layouts across Adobe's products.

How do I make my grid page responsive?

Building a Responsive Grid-View First ensure that all HTML elements have the box-sizing property set to border-box . This makes sure that the padding and border are included in the total width and height of the elements. Read more about the box-sizing property in our CSS Box Sizing chapter.


2 Answers

I just had to do that and I could solve it with simple react Views and the flexWrap rule.

Parent view

{ flex: 1, flexDirection: 'row', flexWrap: 'wrap'}

Child View (Items)

{ width: '50%' }

Hope that this might help someone!

like image 158
ius Avatar answered Sep 16 '22 15:09

ius


It's very simple, Use flex box, In your render method return view contains rows, and use for loop for putting data.

const rowData=[];

render() {
    return <View style={styles.container}>{rowData}</View>;
}

for (let objectData of newArray) {
    rowData.push(this.renderRowView(objectData));
}

renderRowView(objectData) {
    return(
        <View style={styles.mainContainer}>
            <View style={styles.leftView}></View>
            <View style={styles.rightView}></View>
       </View>
   );
}

const styles = StyleSheet.create({
    mainContainer: {
        flex:1,
    },
    leftView: {
        flex:1,
    },
    rightView: {
        flex:1,
    }
});

I have not run this code but hope it works for you!!!

like image 32
Harish Pathak Avatar answered Sep 17 '22 15:09

Harish Pathak