Reputation: 449
I have a component that may have many items or only one item, and I want when I have one item to take the full width and when more than one item take a specific width number ?
Is that possible ? If yes How ?
Upvotes: 0
Views: 1099
Reputation: 1866
To render FlatList items you need to pass an array to it, check if the length of that array equals to one item only or has more items and add the width based on it like this example :
const [arrayOfItems,setArrayOfItems] = useState(['Lonely Item']);
const widthCondition = arrayOfItems.length === 1 ? '100%' : '20%';
<FlatList
data={arrayOfItems}
renderItem={({item}) =>
<View style={{width:widthCondition}}>
<Text>{item}</Text>
</View>
}
ItemSeparatorComponent={this.renderSeparator}
/>
Upvotes: 2