<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ flatlist - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ flatlist - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Mon, 18 May 2026 10:48:20 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/flatlist/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to build a React Native FlatList with realtime searching ability ]]>
                </title>
                <description>
                    <![CDATA[ By Vikrant Negi If you have ever used a mobile app or build one, then you must have come across some kind of list — whether it was a long list of contacts, products, countries, or other things. They may seem very simple to an end user. But for develo... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-a-react-native-flatlist-with-realtime-searching-ability-81ad100f6699/</link>
                <guid isPermaLink="false">66c34fac9972b7c5c7624eb1</guid>
                
                    <category>
                        <![CDATA[ flatlist ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Productivity ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React Native ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 21 Aug 2018 19:10:08 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*3ogqzQjYCBUi_y1WYGLxtA.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Vikrant Negi</p>
<p>If you have ever used a mobile app or build one, then you must have come across some kind of list — whether it was a long list of contacts, products, countries, or other things.</p>
<p>They may seem very simple to an end user. But for developers, displaying a long list of data has always been a pain point when it comes to performant long lists. This is especially true in the apps that are build with React Native.</p>
<h3 id="heading-background">Background</h3>
<p>During the initial days of React Native, we had the good old <code>[ListVie](https://facebook.github.io/react-native/docs/listview)w</code>. It had many features which made it very attractive, and it was a default choice for efficiently displaying vertical list of changing data.</p>
<p>With time, however, many issues and bugs came up, and there was a point when the React Native team realised that it was time to reinvent the wheel.</p>
<h4 id="heading-enter-flatlist">Enter FlatList</h4>
<p>On March 2017, Facebook introduced the <code>[FlatList](https://facebook.github.io/react-native/docs/flatlist)</code> component, which is an easier and more performant way to implement simple, performant lists. Not only that — its API is easier to understand than the original<code>ListView</code>. Here is how a simple FlatList looks:</p>
<pre><code>&lt;FlatList   data={[{<span class="hljs-attr">title</span>: ‘Title Text’, <span class="hljs-attr">key</span>: ‘item1’}, …]}   renderItem={<span class="hljs-function">(<span class="hljs-params">{item}</span>) =&gt;</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ListItem</span> <span class="hljs-attr">title</span>=<span class="hljs-string">{item.title}</span> /&gt;</span></span>} /&gt;
</code></pre><p>Apart from <code>FlatList</code>, you can also use <a target="_blank" href="https://facebook.github.io/react-native/docs/sectionlist"><code>SectionList</code></a> for rendering sectioned lists.</p>
<h4 id="heading-whats-next">What’s next</h4>
<p>As I mentioned earlier, ListView was used primarily for displaying long lists of vertical changing data. But long lists of data are as useful as a hammer without the handle. ?</p>
<p>Almost all the time, whenever you encounter a long list of data, you are also presented with the ability to search though that data so that you don’t get lost searching.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/tRc29TnV5CmgAoO8moaZ79moeGeTUVTbBdj9" alt="Image" width="359" height="638" loading="lazy">
<em>Whatsapp country list with search</em></p>
<h3 id="heading-react-native-searchable-flatlist">React Native Searchable FlatList</h3>
<p>I decided to build something to solve this problem. You can find the complete project repo <a target="_blank" href="https://github.com/vikrantnegi/react-native-searchable-flatlist">here</a>.</p>
<p>If you are not familiar with the FlatList, I would recommend going through the basics of FlatList first. <a target="_blank" href="https://medium.com/react-native-development/how-to-use-the-flatlist-component-react-native-basics-92c482816fe6">This article</a> one by Spencer Carli is the best for beginners that are new to React Native.</p>
<p>And now, without any further ado, let’s get started and make our searchable FlatList!</p>
<p>First, let’s set some initial states that we are going to use later in the project:</p>
<pre><code><span class="hljs-built_in">this</span>.state = {
  <span class="hljs-attr">loading</span>: <span class="hljs-literal">false</span>,      
  <span class="hljs-attr">data</span>: [],      
  <span class="hljs-attr">error</span>: <span class="hljs-literal">null</span>,    
};
</code></pre><p>We’ll also need an array variable:</p>
<pre><code><span class="hljs-built_in">this</span>.arrayholder = [];
</code></pre><p>Apparently an empty list is no fun. So let spice it up with some random list of users.</p>
<p>We are going to user <a target="_blank" href="https://randomuser.me/">randomuser.me</a> which is a free, <a target="_blank" href="https://github.com/RandomAPI/Randomuser.me-Node">open-source</a> API for generating random user data. Its like Lorem Ipsum, but for people.</p>
<p>Let’s create a function that goes and fetches some user data for us.</p>
<pre><code>makeRemoteRequest = <span class="hljs-function">() =&gt;</span> {    
  <span class="hljs-keyword">const</span> url = <span class="hljs-string">`https://randomuser.me/api/?&amp;results=20`</span>;
  <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">loading</span>: <span class="hljs-literal">true</span> });

  fetch(url)      
    .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> res.json())      
    .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> {        
      <span class="hljs-built_in">this</span>.setState({          
        <span class="hljs-attr">data</span>: res.results,          
        <span class="hljs-attr">error</span>: res.error || <span class="hljs-literal">null</span>,          
        <span class="hljs-attr">loading</span>: <span class="hljs-literal">false</span>,        
      });        

     <span class="hljs-built_in">this</span>.arrayholder = res.results;      
   })      
   .catch(<span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span> {        
     <span class="hljs-built_in">this</span>.setState({ error, <span class="hljs-attr">loading</span>: <span class="hljs-literal">false</span> });      
   });  
};
</code></pre><p>In the above code snippet, we are using the <code>[fetch](https://facebook.github.io/react-native/docs/network)</code> API to make a remote API request. When the request is complete, we will receive the user data which is saved to <code>data</code> state and also to our <code>arrayholder</code>.</p>
<p>Now, for the user to search the list, we need to add a search bar on the top of the <code>FlatList</code>. <code>FlatList</code> has a prop to add any custom component to its heade,r which is useful as we’ll be adding a search component there.</p>
<pre><code>renderHeader = <span class="hljs-function">() =&gt;</span> {    
  <span class="hljs-keyword">return</span> (      
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">SearchBar</span>        
      <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Type Here..."</span>        
      <span class="hljs-attr">lightTheme</span>        
      <span class="hljs-attr">round</span>        
      <span class="hljs-attr">onChangeText</span>=<span class="hljs-string">{text</span> =&gt;</span> this.searchFilterFunction(text)}
      autoCorrect={false}             
    /&gt;</span>    
  );  
};
</code></pre><p>In the above function we are using <code>react-native-elements</code> <code>SearchBar</code> component as out header component.</p>
<p>By default, there is no logic which will filter the list as we type inside the <code>SearchBar</code>. For that we’ll need to write a function that will filter the results as the text inside the <code>SearchBar</code> changes.</p>
<pre><code>searchFilterFunction = <span class="hljs-function"><span class="hljs-params">text</span> =&gt;</span> {    
  <span class="hljs-keyword">const</span> newData = <span class="hljs-built_in">this</span>.arrayholder.filter(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> {      
    <span class="hljs-keyword">const</span> itemData = <span class="hljs-string">`<span class="hljs-subst">${item.name.title.toUpperCase()}</span>   
    <span class="hljs-subst">${item.name.first.toUpperCase()}</span> <span class="hljs-subst">${item.name.last.toUpperCase()}</span>`</span>;

     <span class="hljs-keyword">const</span> textData = text.toUpperCase();

     <span class="hljs-keyword">return</span> itemData.indexOf(textData) &gt; <span class="hljs-number">-1</span>;    
  });

  <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">data</span>: newData });  
};
</code></pre><p>The above function will run the filter function on the <code>arrayholder</code>. We’ll be filtering users based on their name, so we’ll store the name inside the <code>itemData</code> variable and convert it to uppercase.</p>
<p>This function will receive the text that the user types as a parameter, which we will store in another variable textData after converting it to uppercase.</p>
<p>After that, we’ll use the <code>indexOf</code> to compare both the text and return true if the text is found inside the <code>itemData</code>. If a true is returned, than <code>filter</code> will keep that data other wise ignore it. So new data is returned anytime the user types any text in the search bar. This new data is then set to the <code>data</code> state, which will eventually be used as a data source for <code>FlatList</code>.</p>
<p>Now its time to render the FlatList.</p>
<pre><code>&lt;List containerStyle={{ <span class="hljs-attr">borderTopWidth</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">borderBottomWidth</span>: <span class="hljs-number">0</span> }}&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">FlatList</span>          
    <span class="hljs-attr">data</span>=<span class="hljs-string">{this.state.data}</span>          
    <span class="hljs-attr">renderItem</span>=<span class="hljs-string">{({</span> <span class="hljs-attr">item</span> }) =&gt;</span> ( 
      <span class="hljs-tag">&lt;<span class="hljs-name">ListItem</span>              
        <span class="hljs-attr">roundAvatar</span>              
        <span class="hljs-attr">title</span>=<span class="hljs-string">{</span>`${<span class="hljs-attr">item.name.first</span>} ${<span class="hljs-attr">item.name.last</span>}`}  
        <span class="hljs-attr">subtitle</span>=<span class="hljs-string">{item.email}</span>                           
        <span class="hljs-attr">avatar</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">uri:</span> <span class="hljs-attr">item.picture.thumbnail</span> }}   
        <span class="hljs-attr">containerStyle</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">borderBottomWidth:</span> <span class="hljs-attr">0</span> }} 
       /&gt;</span>          
     )}          
     keyExtractor={item =&gt; item.email}  
     ItemSeparatorComponent={this.renderSeparator} 
     ListHeaderComponent={this.renderHeader}                             
  /&gt;</span>            
&lt;/List&gt;
</code></pre><p>That is all we need to do. Hurray!!</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/PFxaKuUElMhRBp1cJh2BMtcWlsuCMXWBd588" alt="Image" width="288" height="582" loading="lazy">
<em>React native searchable FlatList</em></p>
<h4 id="heading-closing-thoughts">Closing Thoughts</h4>
<p>I’ve skipped some code that is not so important for this tutorial and for the sake of brevity. You can find the full working <a target="_blank" href="https://github.com/vikrantnegi/react-native-searchable-flatlist">repo</a> on GitHub.</p>
<p>Also, I believe there can be other ways to achieve the same — so if you do find another way, then please feel free to share it.</p>
<p>You can also follow me on <a target="_blank" href="https://twitter.com/vikrant_negi">Twitter</a> and <a target="_blank" href="https://github.com/vikrantnegi/">GitHub</a>. And check out my previous articles in <a target="_blank" href="https://medium.com/@vikrantnegi">Medium</a>.</p>
<p>Other Helpful Articles:</p>
<ul>
<li><a target="_blank" href="https://medium.com/quick-code/react-native-location-tracking-14ab2c9e2db8">React Native Location Tracking</a></li>
<li><a target="_blank" href="https://medium.freecodecamp.org/how-to-build-react-native-charts-with-dynamic-tooltips-64aefc550c95">React Native charts with dynamic tooltips</a></li>
<li><a target="_blank" href="https://medium.freecodecamp.org/how-to-build-react-native-charts-with-dynamic-tooltips-64aefc550c95">React Native charts with dynamic tooltips</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
