Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FlatList Dynamic Height Sizing

Tags:

I have an AutoComplete Box which gives me a list of autocomplete items. I display the items in FlatList, I also have a border around the FlatList. My code is given below: -

render(){
return (
  <View>
    <TextInput
       clearButtonMode="while-editing"
       onChangeText={this.onChangeText}
       value={this.state.searchText}
       onSubmitEditing={this.onTextSubmitted}
       placeholder="Find..." />
       {this.state.data.length > 0 &&
        <FlatList
           style={styles.list}
           keyboardShouldPersistTaps="handled"
           data={this.state.data}
           ItemSeparatorComponent={this.renderSeparator}
           keyExtractor={item => item.properties.id}
           renderItem={this.renderItem} />});
}

const styles = StyleSheet.create({
list: {
    borderWidth: 16,
    borderColor: colors.searchBorder,
  },
});

How can I increase/decrease the size of the FlatList with the number of list items, (I think the border is the reason behind this error).

Sample Image

like image 441
aprofromindia Avatar asked Sep 05 '17 14:09

aprofromindia


People also ask

How does FlatList get content height?

You can get the height of a view using the onLayout prop. Animating height is not the best approach as height animations are not performant. You may wish to animate a transform instead and animate the vertical scale of the view. Not everything you can do with Animated is currently supported by the native driver.

How do you optimize a FlatList performance?

The more complex your components are, the slower they will render. Try to avoid a lot of logic and nesting in your list items. If you are reusing this list item component a lot in your app, create a component only for your big lists and make them with as little logic and nesting as possible.

What is extra data in FlatList?

extraData takes a boolean value to re-render the current list. FlatList is a PureComponent that does not re-render against any change in the state or props objects, so extraData prop is used to re-render the current list if the data array is being mutated.


2 Answers

add flexGrow: 0, and minHeight as per your requirement

 container: {
        flex: 1,
        flexGrow: 0,
        minHeight: 70,
        flexDirection: 'row',
        alignItems: 'center',          
    },
like image 31
Keshav Gera Avatar answered Sep 23 '22 18:09

Keshav Gera


Add flexGrow: 0 to your list style.

like image 192
Julian K Avatar answered Sep 23 '22 18:09

Julian K