React-native-paper: Searchbar keyboard closes after each entry in FlatList ListHeaderComponent

Created on 18 Jan 2019  路  4Comments  路  Source: callstack/react-native-paper


Current behaviour

when the text changes (onChangeText) in Searchbar the keyboard closes

Expected behaviour

I expect the keyboard will not close after changing the text

Code sample

data={works}
keyExtractor={item => item.id.toString()}
renderItem={this._renderItem}
ListHeaderComponent={() => (
placeholder="袩芯懈褋泻"
onChangeText={query => {
this.setState({ firstQuery: query });
}}
value={firstQuery}
style={{ margin: 16, marginBottom: 0 }}
/>
)}
refreshing={refreshing}
onRefresh={this._onRefresh}
ListFooterComponent={() => }
/>

Screenshots (if applicable)

bug jn4ik 1

What have you tried

Your Environment

| software | version
| --------------------- | -------
| ios or android | android
| react-native | https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz
| react-native-paper | ^2.2.8
| node | v10.15.0
| npm or yarn | yarn 1.13.0
| expo sdk | ^32.0.0

Most helpful comment

Hello @makhataibar , try render ListHeaderComponent directly as a React Element instead of a rendering a function.

        <FlatList
          ...
          ListHeaderComponent={
            <Searchbar
              placeholder="袩芯懈褋泻"
              onChangeText={query => {
                this.setState({ firstQuery: query });
              }}
              value={firstQuery}
              style={{ margin: 16, marginBottom: 0 }}
            />
          }
        />

More about that issue you can read here

All 4 comments

Hello @makhataibar , try render ListHeaderComponent directly as a React Element instead of a rendering a function.

        <FlatList
          ...
          ListHeaderComponent={
            <Searchbar
              placeholder="袩芯懈褋泻"
              onChangeText={query => {
                this.setState({ firstQuery: query });
              }}
              value={firstQuery}
              style={{ margin: 16, marginBottom: 0 }}
            />
          }
        />

More about that issue you can read here

Basically FlatList expects you to pass a React Component (as evident by the name ListHeaderComponent). When you pass an inline function like that, it creates a new component every render. When the component signatures differ (in this case the function reference), react will unmount the previous tree with the old component and mount a new tree with the new component. Due to this, all of the local state of the previous component is lost, such as focus state.

I fixed it by adding below line on my Flatlist component:
keyExtractor={item => item}
...
/>

https://stackoverflow.com/a/61932641/10912970
this answer helped me fix the issue.

<FlatList
....
removeClippedSubviews={false}
...
/>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ButuzGOL picture ButuzGOL  路  3Comments

ButuzGOL picture ButuzGOL  路  4Comments

zachariahtimothy picture zachariahtimothy  路  3Comments

tonyxiao picture tonyxiao  路  3Comments

ferrannp picture ferrannp  路  4Comments