ColumnWrapperStyle example #804
-
Hi guys, I don't understand how to achieve columnWrapperStyle with FlashList. I have checked other discussions and tried playing with the overrideItemLayout prop, but I am not getting the desired result Given this code with FlatList where I have 2 columns with the items touching the edge of the screen, how do I achieve the same using FlashList? <FlatList
columnWrapperStyle={{
justifyContent: 'space-between'
}}
keyExtractor={item => item.id}
data={data}
numColumns={2}
renderItem={Item}
/> Here's a minimal snack showing what I am getting with both implementations: |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
cc: @hirbod |
Beta Was this translation helpful? Give feedback.
-
Solved this by creating a helper component and calculating the alignment of each column based on the index import { StyleSheet, View, ViewProps } from "react-native";
const getItemStyle = (index: number, numColumns: number) => {
const alignItems = (() => {
if (numColumns < 2 || index % numColumns === 0) return "flex-start";
if ((index + 1) % numColumns === 0) return "flex-end";
return "center";
})();
return {
alignItems,
width: "100%",
} as const;
};
type ColumnItemProps = ViewProps & {
children: React.ReactNode;
index: number;
numColumns: number;
};
export const ColumnItem = ({ children, index, numColumns, ...rest }: ColumnItemProps) => (
<View style={StyleSheet.flatten([getItemStyle(index, numColumns),rest.style])} {...rest}>
{children}
</View>
); You can then use the component in your ...
const numColumns = 3
...
<FlashList
data={data}
numColumns={numColumns}
renderItem={({index}) => (
<ColumnItem index={index} numColumns={numColumns}>
<CardContent />
</ColumnItem>
)}
/>
.... |
Beta Was this translation helpful? Give feedback.
-
thx bro i need this! |
Beta Was this translation helpful? Give feedback.
-
how to add spaces between the list while keeping them centered? |
Beta Was this translation helpful? Give feedback.
Solved this by creating a helper component and calculating the alignment of each column based on the index