Paul
Paul

Reputation: 929

React Native "keyboardDismissMode" at FlatList

Is there any possibility to prevent the keyboard from dismissing when scrolling a FlatList?

When using a ScrollView setting the prop "keyboardDismissMode" to "none" is the solution to this problem, but this doesn't work for me at a FlatList...

I use the FlatList inside a self-made component, that is in a Stack-Navigator, while there is a focussed TextInput in its header. I render the FlatList like this:

<View style={{flex: 1}}>
  <FlatList 
    style={{flex: 1}}
    data={this.props.data}
    keyExtractor={(item, index) => item.id}
    renderItem={this.renderItem}
  />
</View>

The renderItem() function:

renderItem = ({item, index}) => (
  <TouchableHighlight
    style={{paddingVertical: 10}}
    onPress={() => {
      this.props.onChooseItem(item);
    }}
  >
    <Text numberOfLines={1} >
      {item.text}
    </Text>
  </TouchableHighlight>
)

Upvotes: 3

Views: 10240

Answers (3)

suther
suther

Reputation: 13588

You might think about to encapsulate your FlatList in a ScrollView?

Even if this seems to solve the issue, it's NOT a recommended way!

That's because if it force rerendering the whole flatlist, each time you scroll the screen.

You might better try a component like react-native-keyboard-aware-scroll-view

I've found this article with some alternate Ideas to fix it:
How to use KeyboardAvoidingView with FlatList?

Check: https://facebook.github.io/react-native/docs/scrollview#keyboarddismissmode

Upvotes: 0

Rajesh N
Rajesh N

Reputation: 6693

No need of scrollview inside flatlist it will create performance issue.

just add onScrollBeginDrag={Keyboard.dismiss} in flatlist. it will work in android as well iOS while keyboardDismissMode='on-drag' will work only in iOS

Upvotes: 1

rfii
rfii

Reputation: 593

The docs at the beginning of the reference section says that FlatList "Inherits ScrollView Props, unless it is nested in another FlatList of same orientation."
So I think you can just do use that keyboardDismissMode without encapsulation in a scrollview.

Upvotes: 1

Related Questions