I'm using FlatList from React Native to render a bunch of items stacked on top of each other.
This works fine, I've managed to make the FlatList
itself expand it's height to fill the available space but I want the FlatList
items to grow when there is more space available.
This was simple to do using ScrollView (simplified pseudocode):
<ScrollView
contentContainerStyle={{
display: "flex",
flexGrow: 1,
}}
>
<Item key={1} style={{ flexGrow: 1 }} />
<Item key={2} style={{ flexGrow: 1 }} />
</ScrollView>
However it doesn't work for FlatList (simplified pseudocode):
<FlatList
contentContainerStyle={{
display: "flex",
flexGrow: 1,
}}
renderItem={({ index }) => <Item key={index} style={{ flexGrow: 1 }} />}
/>
I've created an Expo Snack showing both the ScrollView
successfully growing the elements and the FlatList
failing to do so.
There seems to be a lot of discussion online about making the parent FlatList
full height, but I've struggled to find anything about making the FlatList
items grow to fill the available FlatList
height.
I need to use FlatList
as I plan to use react-native-draggable-flatlist to implement drag and drop and cannot find an equivalent library that doesn't use FlatList
.
A screenshot of the Expo Snack is shown here:
As seen here, it seems the offending issue in the DOM is the CallRenderer
, the VirtualizedList
is the right height with the right flex styling, and DayItem
is set to grow (and worked fine with ScrollView
), but in between there's the CallRenderer
which seems to interrupt the flex relationship between the two:
Any help is much appreciated.
to set the height of the View to 300px to make the height of the FlatList 300px. To change the height of a FlatList in React Native, we can set the height of the View that we wrap around the FlatList.
You will normally use a combination of flexDirection, alignItems, and justifyContent to achieve the right layout. Flexbox works the same way in React Native as it does in CSS on the web, with a few exceptions. The defaults are different, with flexDirection defaulting to column instead of row, and the flex parameter only supporting a single number.
In the following example the red, yellow and the green views are all children in the container view that has flex: 1 set. The red view uses flex: 1 , the yellow view uses flex: 2 and the green view uses flex: 3 . 1+2+3 = 6 which means that the red view will get 1/6 of the space, the yellow 2/6 of the space and the green 3/6 of the space.
auto (default value) React Native calculates the width/height for the element based on its content, whether that is other children, text, or an image. pixels Defines the width/height in absolute pixels. Depending on other styles set on the component, this may or may not be the final dimension of the node.
As a workaround, you can listen to onLayout
of the Flatlist, hold the calculated height in a state, and then set each child's height to flatlist height / data.length.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With