<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nnamdi Jibunoh</title>
    <description>The latest articles on DEV Community by Nnamdi Jibunoh (@moschap).</description>
    <link>https://dev.to/moschap</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F326199%2F5918efde-ef79-4938-bfd0-018e3e381e54.jpeg</url>
      <title>DEV Community: Nnamdi Jibunoh</title>
      <link>https://dev.to/moschap</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/moschap"/>
    <language>en</language>
    <item>
      <title>HowTo: Alternative way to display placeholder for empty React Native FlatList </title>
      <dc:creator>Nnamdi Jibunoh</dc:creator>
      <pubDate>Wed, 29 Jan 2020 14:04:37 +0000</pubDate>
      <link>https://dev.to/moschap/howto-alternative-way-to-display-placeholder-for-empty-react-native-flatlist-3b9p</link>
      <guid>https://dev.to/moschap/howto-alternative-way-to-display-placeholder-for-empty-react-native-flatlist-3b9p</guid>
      <description>&lt;p&gt;If you are in a big hurry and would like to skip some sections, use this.&lt;/p&gt;

&lt;h1&gt;
  
  
  Table of contents
&lt;/h1&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    * Introduction
    * Popular way to display empty FlatList placeholder
    * Better(native) empty FlatList placeholder
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Introduction &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;If you are a mobile application developer, you will quickly come to the realization that most of the views that you build will have List components driving them. These views can have Lists, in vertical or horizontal orientation or a combination of the 2 of them. This is true whether you are building native or hybrids apps.&lt;/p&gt;

&lt;p&gt;In React-Native there are a couple of solutions (ex. &lt;a href="https://facebook.github.io/react-native/docs/scrollview"&gt;ScrollView&lt;/a&gt;, &lt;a href="https://facebook.github.io/react-native/docs/sectionlist"&gt;SectionList&lt;/a&gt; and &lt;a href="https://facebook.github.io/react-native/docs/flatlist"&gt;FlatList&lt;/a&gt;) for your ListView requirements and one of the popular options and the one that we will be looking at is &lt;strong&gt;FlatList&lt;/strong&gt; (&lt;strong&gt;Note:&lt;/strong&gt; this tutorial also applies to SectionList)&lt;/p&gt;
&lt;h2&gt;
  
  
  Popular way to display empty FlatList placeholder &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;I have used this method personally until recently to manage my List while developing React-Native apps to show a placeholder when the data array supplied to a FlatList is empty.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render() {
    return (
      &amp;lt;View style={styles.container}&amp;gt;
        {this.data.length === 0 &amp;amp;&amp;amp; (
          &amp;lt;View
            style={{
              borderWidth: 1,
              height: '50%',
              flex: 1,
              justifyContent: 'center',
              alignItems: 'center',
            }}&amp;gt;
            &amp;lt;Text&amp;gt;No Airport detail available&amp;lt;/Text&amp;gt;
          &amp;lt;/View&amp;gt;
        )}

        {this.data.length &amp;gt; 0 &amp;amp;&amp;amp; (
          &amp;lt;FlatList
          style={{ flex: 1 }}
          data={[]}
          ItemSeparatorComponent={() =&amp;gt; this.listSeparator()}
          renderItem={({ item }) =&amp;gt; (
            &amp;lt;View style={{ paddingHorizontal: 20, paddingVertical: 10 }}&amp;gt;
              &amp;lt;Text style={{ fontSize: 18, fontWeight: '600' }}&amp;gt;
                {item.name}
              &amp;lt;/Text&amp;gt;
              &amp;lt;Text style={{ fontSize: 14, color: 'gray' }}&amp;gt;
                {item.shortName}
              &amp;lt;/Text&amp;gt;
            &amp;lt;/View&amp;gt;
          )}
        /&amp;gt;
        )}

      &amp;lt;/View&amp;gt;
    );
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is popular because it is the default way to conditionally display content in React(Native). From the code you can see that we conditionally render the placeholder based on whether the data array is empty or not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Better(native) empty FlatList placeholder &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Event though the conditional option works really well i discovered recently that there is a provided primitive that handles that out of the box, so when the data array is empty it renders your placeholder and when the data comes in eg from the server the FlatList will handle the display internally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;listEmptyComponent = () =&amp;gt; (
    &amp;lt;View
      style={{
        borderWidth: 1,
        height: '50%',
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
      }}&amp;gt;
      &amp;lt;Text&amp;gt;No Airport detail available&amp;lt;/Text&amp;gt;
    &amp;lt;/View&amp;gt;
  );

render() {
    return (
      &amp;lt;View style={styles.container}&amp;gt;
        {this.data.length &amp;gt; 0 &amp;amp;&amp;amp; (
          &amp;lt;FlatList
          style={{ flex: 1 }}
          data={[]}
          ListHeaderComponent={() =&amp;gt; this.listHeader()}
          ListEmptyComponent={() =&amp;gt; this.listEmptyComponent()}
          ItemSeparatorComponent={() =&amp;gt; this.listSeparator()}
          renderItem={({ item }) =&amp;gt; (
            &amp;lt;View style={{ paddingHorizontal: 20, paddingVertical: 10 }}&amp;gt;
              &amp;lt;Text style={{ fontSize: 18, fontWeight: '600' }}&amp;gt;
                {item.name}
              &amp;lt;/Text&amp;gt;
              &amp;lt;Text style={{ fontSize: 14, color: 'gray' }}&amp;gt;
                {item.shortName}
              &amp;lt;/Text&amp;gt;
            &amp;lt;/View&amp;gt;
          )}
        /&amp;gt;
        )}

      &amp;lt;/View&amp;gt;
    );
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the FlatList component we supply the aptly named parameter &lt;strong&gt;ListEmptyComponent&lt;/strong&gt; with a function component that will be displayed when the FlaatList is empty. &lt;/p&gt;

&lt;p&gt;If you love concise code then this may not appeal to you, because obviously the code is not shorter than the previous method, but an obvious advantage is how it can help clean up the &lt;code&gt;render()&lt;/code&gt;, a reason i hope can nudge you towards using it in your code in the near future.&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
