React Native - Using "flex" inside a FlatList

Viewed 3578

I'm starting to learn React Native, and I'm trying to create grid of 3 columns with images. I've been using the numColumns prop of the FlatList to specify 3 columns, and then setting flex:1 for my images so they should fill the space of the column. However flex:1 makes none of my images appear, while trying height:100,aspectRatio:1 shows all of my images in columns. Any idea why this is? My code is down below:

export default class ArtScrollView extends React.Component {
    _renderItem = (item) =>
        (
            <Image style={styles.art} source={{uri:item.item.imgFilePath}}/>
        )
    render() {
        return(
            <FlatList numColumns={3}
            data={Object.values(this.props.pods)}
            renderItem={this._renderItem}/>
        );
    }
}

const styles = StyleSheet.create({
    art:{
        height:100,
        aspectRatio:1,
        //flex:1, <- Having this instead of specifying the height doesn't work
        marginRight:10,
    }
  });
1 Answers
  • Auto-sizing images in ReactNative does not work. It just doesn't. You need to know the dimensions of images before you show them.
  • It is quite easy to use var {height, width} = Dimensions.get('window'); and use those as your reference for your images' sizing.
  • Use the inspector and see (and understand) what FlatList actually renders for you. You may understand why things do not work (not just in this case but generally)
Related