below code works fine where there are only two elements in ScrollView container.
but if there are more items they remain in a single row despite giving them a flex: wrap property
import React, { Component } from 'react';
import { Button, View, StyleSheet, ScrollView } from 'react-native';
export default class GridView extends Component {
render() {
return (
<ScrollView contentContainerStyle={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Button 1"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Button 2"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Button 1"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Button 2"/>
</View>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
alignItems: 'center',
justifyContent: 'center',
padding: 10,
gap: 10,
},
buttonContainer: {
flex: 1,
}
});
2
Answers
Remove "flex: 1" from buttonContainer and change styles of container according to below code.
My suggestion is to do something like this,
pass all the buttons to GridView component from the parent component
inside the Gridview transform your buttons array to something like this.
above you have paired two buttons into subarrays. Then do something like below,