question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FlatList] Issues Rendering a TextInput within FlatList > ListHeaderComponent

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:19
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

17reactions
joncursicommented, Sep 26, 2017

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 ListHeaderComponent directly as a React Element instead of a render function:

      <FlatList
        ...
        data={[...]}
        ListHeaderComponent={
          <TextInput
            ...
            onChangeText={(value: string): void => this._onChangeText(value)}
            value={this.state.query}
            ...
          />
        }
        ...
      />

This enables React to properly diff the ListHeaderComponent prop across changes, keeping the original TextInput mounted and just changing the value.

5reactions
dborzoueicommented, Jan 25, 2018

just use “Arrow functions” like this sample

...

renderHeader = () => {
   return <TextInput
            ...
            onChangeText={(name): this.setState({name})}
            value={this.state.name}
            ...
          />
}

...

 <FlatList
        ...
        data={[...]}
        ListHeaderComponent={this.renderHeader()}
        ...
      />

It works for me or just rendered the ListHeaderComponent directly as a React Element instead of a render function

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found