gpt-4o-2024-05-13· OpenAI
WildChat-4.8M (AI2)
<View style={{ flexDirection: 'row' }}> {posts.map((x, i) => { return <FoodPost key={i}…
- User
<View style={{ flexDirection: 'row' }}> {posts.map((x, i) => { return })}
All items in the loop line up in a horizontal row. How do I make it so that it's 3 per line, take as many lines needed using some css properties
- Assistant
To display your items in a grid layout with 3 items per row, you can use the
flex-wrapandflex-basisproperties from CSS. Here is how you can adjust your component to achieve that:javascript<View style={{ flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'space-between' // Optional: Adjust item spacing horizontally }}> {posts.map((x, i) => ( <View key={i} style={{ width: '30%', marginBottom: 10 }}> <FoodPost /> </View> ))} </View>Explanation:
flexDirection: 'row'allows the items to be laid out in a row (horizontally).flexWrap: 'wrap'allows the items to wrap onto multiple lines if they exceed the width of the container.- Each item is wrapped in a
<View>component with awidthof'30%', which ensures that three items fit in one row (30% * 3 = 90%, leaving space for some margin or padding).