[FlatList] Issues Rendering a TextInput within FlatList > ListHeaderComponent
See original GitHub issueI am trying to render a TextInput at the top of a FlatList so that the user can search / filter the list of data.
      <FlatList
        ...
        data={[...]}
        ListHeaderComponent={(): React$Element<*> => (
          <TextInput
            ...
            onChangeText={(value: string): void => this._onChangeText(value)}
            value={this.state.query}
            ...
          />
        )}
        ...
      />
Every time the value prop given to TextInput changes, the keyboard dismisses rather than staying active and allowing the user to type. It looks as though the entire TextInput component is unmounted and re-mounted, forcing this odd behavior.
I’ve used this pattern on ListView’s renderHeader with great success. Would like to see it working on FlatList as well!
Issue Analytics
- State:
- Created 6 years ago
- Reactions:19
- Comments:20 (6 by maintainers)
 Top Results From Across the Web
Top Results From Across the Web
TextInput lost focus after typing one symbol when searching
The auto-bound method doesn't work since the ListHeaderComponent re-renders when data is becomes an empty array. I used the following work- ...
Read more >React Native — SearchBar in FlatList loses focus after typing
The problem I encountered is that after typing one character the search bar loses focus and the keyboard disappears — it's almost like...
Read more >A deep dive into React Native FlatList - LogRocket Blog
FlatList is a React Native component that allows you to render lists with zero hassle and minimal code. Learn how to customize FlatList....
Read more >FlatList - React Native
This includes the data prop and parent component state. In order to constrain memory and enable smooth scrolling, content is rendered ...
Read more >Displaying a List in React Native: Map Method or FlatList ...
Scroll loading; Scroll to a specific position in the list; Multiple column support. Under the hood, FlatList uses the ScrollView component to render...
Read more > Top Related Medium Post
Top Related Medium Post
No results found
 Top Related StackOverflow Question
Top Related StackOverflow Question
No results found
 Troubleshoot Live Code
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free Top Related Reddit Thread
Top Related Reddit Thread
No results found
 Top Related Hackernoon Post
Top Related Hackernoon Post
No results found
 Top Related Tweet
Top Related Tweet
No results found
 Top Related Dev.to Post
Top Related Dev.to Post
No results found
 Top Related Hashnode Post
Top Related Hashnode Post
No results found

I’m happy to report that I was able to get this to work with a rather simple fix! Using the same example as posted in the original comment, I just rendered the
ListHeaderComponentdirectly as a React Element instead of a render function:This enables React to properly diff the
ListHeaderComponentprop across changes, keeping the originalTextInputmounted and just changing thevalue.just use “Arrow functions” like this sample
It works for me or just rendered the ListHeaderComponent directly as a React Element instead of a render function