<?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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Linas M on Medium]]></title>
        <description><![CDATA[Stories by Linas M on Medium]]></description>
        <link>https://medium.com/@linasmnew?source=rss-251edff4812d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*o5zE5Uwd5ORf3ao1ks1Xvg.jpeg</url>
            <title>Stories by Linas M on Medium</title>
            <link>https://medium.com/@linasmnew?source=rss-251edff4812d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 12 Nov 2026 08:28:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@linasmnew/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How does CSS fit into Reactjs?]]></title>
            <link>https://medium.com/hackernoon/how-does-css-fit-into-react-5a4aa4840135?source=rss-251edff4812d------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a4aa4840135</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Linas M]]></dc:creator>
            <pubDate>Wed, 14 Feb 2018 00:09:43 GMT</pubDate>
            <atom:updated>2018-02-15T21:41:46.796Z</atom:updated>
            <content:encoded><![CDATA[<p>There are a lot of different ways in which you can style your React applications, and I would even go as far as to say that you actually have <strong><em>more</em></strong> options for styling React applications than traditional web applications. So without further lets list some of these options out:</p><ul><li>Standard CSS</li><li>Inline Styles</li><li>CSS Modules</li><li>CSS-in-JS</li></ul><p>Now let’s take a closer look at each one of these.</p><h4><strong>Standard CSS</strong></h4><p>This is the traditional and the most common way of writing CSS. You create a <strong><em>style.css</em></strong> file, add all of your styles inside of it, then import the stylesheet and reference the styles within your JSX via className or id attributes.</p><p>Note that you don’t have to limit your styles within a single stylesheet, you can just as well split them into multiple files and place them next to the components which they are supposed to target.</p><p>Also note that you can keep on using your everyday CSS libraries and frameworks such as <strong><em>SASS</em></strong>, <strong><em>LESS</em></strong>, <strong><em>Bootstrap</em></strong>, <strong><em>Semantic UI</em></strong>, etc., as well, React doesn’t limit you in any way.</p><h4>Inline Styles</h4><p>Inline styles refer to writing CSS directly inside of HTML markup via the style attribute. In React, using JSX, this looks like this:</p><p>&lt;p style={{color: &quot;red&quot;}}&gt;Hello World&lt;/p&gt;</p><p>Inline styles are probably the most limited approach in terms of styling full blown applications since they cannot make use of all of the CSS features such as <strong><em>pseudo selectors</em></strong>, <strong><em>animations</em></strong> and <strong><em>media queries</em></strong>.</p><p>In practise they are mostly used for basic style modifications such as color toggling based on a condition, but I have also heard of people and even companies using this as their sole styling mechanism.</p><p><em>(Be aware that, arguably, inline styles can also be considered as part of CSS-in-JS, because technically you do write them inside of JS files with React. The reason I chose to put them into their own category was because CSS-in-JS libraries are typically more complex than just writing styles inside of a style attribute — they instead introduce some sort of a higher level abstraction as you will see in the CSS-in-JS section.)</em></p><h4>CSS Modules</h4><p>With CSS modules you create a CSS file for every type of component that you want to style the same way that you might do with the<em> “Standard CSS”</em> option, however the difference here is that with CSS modules the styles are <strong><em>scoped locally</em></strong> which means that unlike <em>“Standard CSS”</em> they <strong>don’t leak into other components</strong>.</p><p>The styles being locally scoped also means that you can forget about implementing your own local scoping strategies or following methodologies such as <a href="http://getbem.com/"><strong><em>BEM</em></strong></a> which require you to manually create and type out long class or id names.</p><p><strong><em>How to use within React:</em></strong></p><p>Use <strong><em>css-loader</em></strong> inside of your webpack.config with modules property set to true, or download a complete library such as <strong><em>css-modules</em></strong>, once setup use as follows:</p><ul><li>create a CSS file for whatever components you want to style</li><li>write your CSS</li><li>import this CSS file inside of the target components</li><li>reference these styles within your components via their className and id attributes</li></ul><p><strong><em>How are CSS Modules implemented behind the scenes?</em></strong></p><p>CSS Modules work by taking your CSS files and running them through a <strong><em>CSS Module compiler</em></strong> which does two things:</p><ul><li>modifies the class and/or id names inside of your CSS files to make them unique</li><li>produces a JS file containing the mapping between the newly generated names and the old names</li></ul><p>Hence why you can use named imports with CSS modules — you are not actually importing your CSS, but rather the JS that was produced by the CSS Module compiler.</p><h4>CSS-in-JS</h4><p>As the name suggests with CSS-in-JS you write your CSS inside of JavaScript files rather than CSS files, how you write and apply them, however, is very dependant on the specific CSS-in-JS library that you use.</p><p>Behind the scenes CSS-in-JS is usually implemented in one of the following three ways <em>(or a combination of)</em>:</p><ul><li>via inline styles</li><li>via a &lt;style&gt; element</li><li>via a &lt;link&gt; element at build time by extracting styles into a CSS file</li></ul><p>For the sake of not making the article overly long, we will only look at a single CSS-in-JS implementation, that is <strong><em>styled-components</em></strong>, which also happens to be one of the most popular implementations out there.</p><p>Before we break down how it works however, let’s first look at an example:</p><pre>import React, { Component } from &quot;react&quot;;<br>import styled from &quot;styled-components&quot;;</pre><pre>const Wrapper = styled.div`<br>  max-width: 600px;<br>  margin: 0 auto;<br>`;</pre><pre>class HelloWorld extends Component {<br>  render() {<br>    return (<br>      &lt;Wrapper&gt;<br>        &lt;p&gt;Hello World&lt;/p&gt;<br>      &lt;/Wrapper&gt;<br>    );<br>  }<br>}</pre><p>Let’s now break this example down. At the top we declare a variable called Wrapper and ask <strong><em>styled-components</em></strong> to create and give us back a div <strong>component</strong> consisting of the styles that we have defined within the backticks — in this case we are asking it for a div component with the following stylesmax-width: 600px; margin: 0 auto;.</p><p>We then add this Wrapper component inside of our render method and use it like a regular div.</p><p>In simple terms all that is going on here is you tell styled-components what kind of a built-in component you want and how it should be styled and styled-components give you that component back.</p><p><strong><em>How do the styles get applied behind the scenes?</em></strong></p><p>Styled-components rely on the&lt;style&gt; element, so internally you can think of it like creating and appending&lt;style&gt; <em>your styles</em> &lt;/style&gt; to the documents head section. Also since it provides local style scoping, it takes care of generating and applying unique class names.</p><p>To better visualise this, let’s run the above code snippet and see what it produces after being added to the DOM:</p><pre>...<br>&lt;head&gt;<br>  ...<br>  &lt;style&gt;<br>    /* sc-component-id: sc-bdVaJa */<br>    .sc-bdVaJa {}<br>    .brPdfu {<br>      max-width: 600px;<br>      margin: 0 auto;<br>    }<br>  &lt;/style&gt;<br>&lt;/head&gt;</pre><pre>&lt;body&gt;<br>  &lt;div id=&quot;app&quot;&gt;<br>    &lt;div class=&quot;sc-bdVaJa brPdfu&quot;&gt;<br>      &lt;p&gt;Hello World&lt;/p&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/body&gt;<br>...</pre><p>As you can see it does look like what we expected it to look like. The only surprise is the extra empty style declaration .sc-bdVaJa {}which based on the comment right above it/* sc-component-id: sc-bdVaJa */seems to suggest that styled-components keep track of which component the styles belong to — presumably for some sort of internal performance optimisation or something along those lines.</p><p>If you like what you see, you might also want to checkout another, very similar, CSS-in-JS library called <strong><em>Emotion</em></strong>, it is very similar to <strong><em>styled-components</em></strong> but gives you the option of extracting all of your styles into a CSS file, at build time, which then get linked into the document via the &lt;link&gt; element. Why is this useful? Because the generated stylesheet can be cached by the browser.</p><p>For a full list of the most popular CSS-in-JS solutions see this <a href="https://github.com/MicheleBertoli/css-in-js">github</a> repository.</p><h4>Finish line</h4><p>Congratulations for getting to the end :), thanks for reading and hope you learned something of use. Twitter: <a href="https://twitter.com/linasmnew">linasmnew</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8d4bSxyE4Hl5dfN4UuJe-w.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a4aa4840135" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/how-does-css-fit-into-react-5a4aa4840135">How does CSS fit into Reactjs?</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing react-graceful-image]]></title>
            <link>https://medium.com/hackernoon/introducing-react-graceful-image-f9125fcefb61?source=rss-251edff4812d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9125fcefb61</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Linas M]]></dc:creator>
            <pubDate>Sun, 21 Jan 2018 00:15:06 GMT</pubDate>
            <atom:updated>2018-02-13T23:53:10.767Z</atom:updated>
            <content:encoded><![CDATA[<p>An image component for gracefully dealing with image errors, by providing optional lazy loading, an optional placeholder and retries on failure. Particularly useful in situations where your application might be used in poor signal areas such as when travelling on a train, a bus or a car.</p><h3>Visual Example</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*3z6opjn3rWEEYKrtZJB2Hw.png" /><figcaption>Browser loading images in various states</figcaption></figure><ol><li>Default browser behaviour when image fails due to bad signal</li><li>With react-graceful-image placeholder</li><li>With react-graceful-image disabled placeholder</li><li>With react-graceful-image retries — if the image fails to load, the package will gracefully re-attempt loading the image again</li></ol><p><em>(note: these are not mutually exclusive, for example the default behaviour makes use of both a placeholder and retries together.)</em></p><h3>Implementation</h3><p><strong>Placeholder</strong></p><ul><li>Implemented as an SVG embedded into a Data URI — this means that you don’t have to fire an extra HTTP request in order to download the placeholder, it is embedded right into the component</li><li>The component also provides a way to customize the placeholder by changing it’s color via the placeholderColor prop — as well as via a regular styles or className props <em>( note that styles passed this way are also passed to the actual image.)</em></li><li>It also gives you the option of disabling the placeholder, in which case the image will not render at all until it is loaded — since the image will not get rendered until it successully loads this also means that incase the image fails it will not take its intended space on the page and you will not get a broken image icon</li></ul><p><strong>Retry</strong></p><ul><li>Implemented using setTimeout which plays around with the delay and the desired number of retries in an attempt to gracefully re-load the image</li><li>By default the delay in between retries doubles on every retry, this can however be modified via the retry.accumulate prop which accepts one of the following values ‘multiply’, ‘add’, ‘noop’ and updates the retry algorithm accordingly <em>(note you can also change the delay time and retry count)</em></li></ul><h3>Usage Example</h3><p>The below code snippet will display a blue SVG placeholder, if it is within the visible viewport then it will load the actual given image and fade it in once it is done loading. If loading the image fails, then it will retry loading the image again for a maximum of 8 times, with initial delay of 2 seconds, which will then increase to 4 seconds, then to 8 seconds, then to 16 seconds, and so on (default retry configuration). It will also apply whatever styles are defined inside of the ‘content-image’ className to the placeholder <em>(and the image once the image loads)</em>.</p><pre>import React, { Component } from &#39;react&#39;<br>import Image from &#39;react-graceful-image&#39;</pre><pre>class YourComponent extends Component {<br>  render() {<br>    return (<br>      &lt;Image<br>        src=&quot;path_to_image&quot;<br>        className=&quot;content-image&quot;<br>        alt=&quot;My awesome image&quot;<br>        placeholderColor=&quot;#0083FE&quot;<br>      /&gt;<br>    );<br>  }<br>}</pre><p>The below code snippet will display a light grey (default) SVG placeholder, if it is within the visible viewport then it will load the actual given image and fade it in once it is done loading. If loading the image fails, then it will retry loading the image again for a maximum of 15 times, with initial delay of 3 seconds which will then increase to 6 seconds, then to 9 seconds, then to 12 seconds, and so on. It will apply 20px of padding to the placeholder <em>(and the image once the image loads).</em></p><pre>import React, { Component } from &#39;react&#39;<br>import Image from &#39;react-graceful-image&#39;</pre><pre>class YourComponent extends Component {<br>  render() {<br>    return (<br>      &lt;Image<br>        src=&quot;path_to_image&quot;<br>        width=&quot;150&quot;<br>        height=&quot;150&quot;<br>        style={{padding: &#39;20px&#39;}}<br>        alt=&quot;My awesome image&quot;<br>        retry={{count: 15, delay: 3, accumulate: &#39;add&#39;}}<br>      /&gt;<br>    );<br>  }<br>}</pre><p>Thanks for reading, for more usage details checkout the projects github page: <a href="https://github.com/linasmnew/react-graceful-image">https://github.com/linasmnew/react-graceful-image</a>. You can also install it via npm using: npm install --save react-graceful-image</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9125fcefb61" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/introducing-react-graceful-image-f9125fcefb61">Introducing react-graceful-image</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React-Native-Navigation (wix) Fix Top Left Corner Flickering]]></title>
            <link>https://medium.com/@linasmnew/react-native-navigation-wix-fix-top-left-corner-flickering-bcf5f9aa57cd?source=rss-251edff4812d------2</link>
            <guid isPermaLink="false">https://medium.com/p/bcf5f9aa57cd</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Linas M]]></dc:creator>
            <pubDate>Mon, 23 Oct 2017 17:10:34 GMT</pubDate>
            <atom:updated>2017-10-23T17:17:34.589Z</atom:updated>
            <content:encoded><![CDATA[<p>React-Native-Navigation has a really annoying problem, where whenever you switch tabs after first opening your application some part of UI in those tabs, for a split second, jumps into to the top left corner of the screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*vno_NMQqG9XXzuoJ68YQrA.jpeg" /></figure><p>There’s an active issue opened regarding this on <a href="https://github.com/wix/react-native-navigation/issues/838">GitHub</a>, however, no solution has yet been implemented to fix this.</p><p>In the mean time, I found a workaround that worked really well for me:</p><p>Add overflow: &#39;hidden&#39; to parent &lt;View&gt; ‘s of all tab components which have this issue, and also to their child components which contain the particular UI that is jumping up.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*U11ovcSwQlMvcj7egt-_-w.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bcf5f9aa57cd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Infinite Scrolling In Firebase]]></title>
            <link>https://medium.com/hackernoon/infinite-scrolling-in-firebase-e28bfbc53578?source=rss-251edff4812d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e28bfbc53578</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Linas M]]></dc:creator>
            <pubDate>Tue, 17 Oct 2017 18:47:57 GMT</pubDate>
            <atom:updated>2018-01-21T00:37:28.606Z</atom:updated>
            <content:encoded><![CDATA[<p>This article will demonstrate you how to implement infinite scrolling in JavaScript and firebase without having to modify your existing firebase data structure.</p><p>I tried to make this article as general and applicable to any framework / library of your choice as possible, so hopefully it gives you a good sense of the steps that you need to go through in order to implement it in your own projects.</p><p>Note: If you don’t want to follow along and are anxious to see the code you can skip to the bottom of the article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7hyRM50LJZP5sgEg3a76Xw.png" /></figure><h4><strong>Outline</strong></h4><p>Before we begin, let’s define the expected behaviour:</p><ul><li>Initial fetch: retrieve the 5 newest items</li><li>Subsequent fetches: retrieve the next 5 newest items</li></ul><p>Let’s also define the test data that we’ll pretend to be fetching from firebase — using fake data will make it easier to visualise what’s going on with the queries:</p><pre>// our firebase database<br>items: { <br>   firstInsertedItem: { … }, // oldest<br>   SecondInsertedItem: { … }, <br>   ThirdInsertedItem: { … },<br>   FourthInsertedItem: { … }, <br>   FifthInsertedItem: { … }, <br>   SixthInsertedItem: { … }, <br>   SeventhInsertedItem: { … },<br>   EighthInsertedItem: { … }, <br>   NinethInsertedItem: { … }, <br>   TenthInsertedItem: { … }, // newest<br> }</pre><pre>// in case you were wondering about the order that I put the items // in, that&#39;s just to make it easier to follow along<br>// and it would make no difference to us if firebase actually <br>// stored those items in a different order internally</pre><h4><strong>Firebase push keys</strong></h4><p>Let’s begin by introducing the magic behind firebase push keys and how they will make all of this possible.</p><p>It turns out that firebase push keys aren’t just some random sequence of characters. In fact all <a href="https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html">firebase push keys</a> consist of a combination of timestamp and random data encoded in a modified base64 alphabet to <strong>preserve</strong> their <strong>chronological</strong> <strong>order</strong> — i.e. their insertion order.</p><p>This is a powerful feature as it will allow us to make use of firebase’s sorting queries.</p><p>Now that we defined this important characteristic we can get on with the tutorial.</p><h4><strong>First step</strong></h4><p>In order for all of our fetches, after the very first one, to return what we expect them to return we will need to keep some sort of a reference to the oldest previously fetched key.</p><p>So let’s define a variable called:</p><pre>let referenceToOldestKey = ‘’;</pre><h4><strong>Initial fetch</strong></h4><p>Next let’s create a query for fetching our first 5 newest inserted items:</p><pre>firebase.database().ref(‘items’)<br> .orderByKey()<br> .limitToLast(5)<br> .once(‘value’)<br> .then((snapshot) =&gt; { … } )<br> .catch((error) =&gt; { … } );</pre><p>This query does the following:</p><p>- <strong><em>orderByKey</em></strong> sorts the items by their keys in chronological order (oldest to newest)<br>- <strong><em>limitToLast</em></strong> selects 5 items from the end (starting with the 5th)</p><p>The returned object will look like this:</p><pre>{ <br>   SixthItemFromTheEnd: { … },<br>   SeventhItemFromTheEnd: { … },<br>   EighthInsertedItem: { … },<br>   NinethInsertedItem: { … },<br>   TenthInsertedItem: { … },<br> }</pre><p>Next we need to reverse the order of this object so that the latest item is on top rather than on the bottom.</p><p>We have several options here, depending on how you like to manage your applications state:</p><ul><li>Reverse the object</li><li>Transform the object into an array of objects and reverse the array</li><li>Keep the object as is but make a shallow copy and reverse on the spot when displaying to the user</li></ul><p>We’ll go with the second option, due to to it being the most popular way of storing this sort of data.</p><p>So let’s add the following to <strong>.then</strong> of our fetch function:</p><pre>let arrayOfKeys = Object.keys(snapshot.val())<br>   .sort()<br>   .reverse();</pre><pre>let results = arrayOfKeys<br>   .map((key) =&gt; snapshot.val()[key]);</pre><p>Note that we’re sorting the keys before reversing their order — this is done to ensure the right order, since JavaScript does not guarantee object key order.</p><p>Next, we need to initialise our reference with the oldest out of the 5 keys so that when the user scrolls to the bottom of the page our next fetch function knows where to carry on from:</p><pre>referenceToOldestKey = arrayOfKeys[arrayOfKeys.length-1];</pre><p>(The oldest key is in last position because remember that <strong><em>limitToLast</em></strong> returns items in chronological order and we reversed it.)</p><p>And, now that we’re finished with the first fetch:</p><pre>// You can do what you want to do with the data, i.e.<br>// append to page or dispatch({ … }) if using redux</pre><h4><strong>Next fetch</strong></h4><p>Ok, our user has just hit the bottom of the page let’s fetch the next 5 newest items for them by creating the following query:</p><pre>firebase.database().ref(‘items’)<br> .orderByKey()<br> .endAt(referenceToOldestKey)<br> .limitToLast(6)<br> .once(‘value’)<br> .then((snapshot) =&gt; { … } )<br> .catch((error) =&gt; { … } );</pre><p>This query does the following:</p><ul><li><strong><em>orderByKey</em></strong> sorts the items by their keys in chronological order (oldest to newest)</li><li><strong><em>endAt</em></strong> selects all items from the 1st one ever added up to our reference variable (<strong>inclusive</strong>)</li><li><strong><em>limitToLast</em></strong> selects 6 items from the end (starting with the 6th)</li></ul><p>The returned object will look like this:</p><pre>{ <br>   firstInsertedItem: { … },<br>   SecondInsertedItem: { … },<br>   ThirdInsertedItem: { … },<br>   FourthInsertedItem: { … },<br>   FifthInsertedItem: { … },<br>   SixthInsertedItem: { … },  // our reference is included!<br> }</pre><ul><li>But why are we limiting to the 6 latest items rather than 5, like we did in the first fetch?</li></ul><p>Because <strong><em>endAt</em></strong> is <strong>inclusive</strong> which means that our <strong>reference key</strong> gets included in the returned object. And so if we limit to 5 we would end up with only 4 new items as 1 would be a duplicate, therefore we need to request for 6 and then handle the removal of the 6th one on the client side.</p><p>Ok, now that we see how the query works let’s reverse the returned object and then remove the duplicate.</p><pre>let arrayOfKeys = Object.keys(snapshot.val())<br>   .sort()<br>   .reverse()<br>   .slice(1);</pre><pre>let results = arrayOfKeys<br>   .map((key) =&gt; snapshot.val()[key]);</pre><p>Why <strong><em>slice(1)</em></strong>? Because after <strong><em>reverse()</em></strong> our duplicate moved from last position into the first one. How do we know it was in last position to begin with? Because remember that the keys were returned in chronological order.</p><p>Lastly, we need to update our reference with the oldest key from the current fetch:</p><pre>referenceToOldestKey = arrayOfKeys[arrayOfKeys.length-1];</pre><p>And, now that we’re finished with the second fetch:</p><pre>// You can do what you want to do with the data, i.e.<br>// append to page or dispatch({ … }) if using redux</pre><p>And this concludes the tutorial, thank you for reading and hopefully you learned something new. Look below to see the full code sample.</p><h4><strong>The complete code sample</strong></h4><p>Note: code duplication inside <strong>.then</strong> of both queries is intentional to make it easier to read, no need to look all over the place to find the relevant code, as would be required if the code was moved out into a separate function.</p><pre>let referenceToOldestKey = ‘’;</pre><pre>if (!referenceToOldestKey) { // if initial fetch<br> <br>  firebase.database().ref(‘items’)<br>   .orderByKey()<br>   .limitToLast(5)<br>   .once(‘value’)<br>   .then((snapshot) =&gt; { </pre><pre>      // changing to reverse chronological order (latest first)<br>      let arrayOfKeys = Object.keys(snapshot.val())<br>         .sort()<br>         .reverse();</pre><pre>      // transforming to array<br>      let results = arrayOfKeys<br>         .map((key) =&gt; snapshot.val()[key]);</pre><pre>      // storing reference<br>      referenceToOldestKey = arrayOfKeys[arrayOfKeys.length-1];<br> <br>      // Do what you want to do with the data, i.e.<br>      // append to page or dispatch({ … }) if using redux</pre><pre>   })<br>   .catch((error) =&gt; { … } );<br> <br> } else {<br> <br>  firebase.database().ref(‘items’)<br>   .orderByKey()<br>   .endAt(oldestKeyReference)<br>   .limitToLast(6)<br>   .once(‘value’)<br>   .then((snapshot) =&gt; {</pre><pre>     // changing to reverse chronological order (latest first)<br>     // &amp; removing duplicate<br>     let arrayOfKeys = Object.keys(snapshot.val())<br>         .sort()<br>         .reverse()<br>         .slice(1);</pre><pre>      // transforming to array<br>      let results = arrayOfKeys<br>         .map((key) =&gt; snapshot.val()[key]);</pre><pre>      // updating reference<br>      referenceToOldestKey = arrayOfKeys[arrayOfKeys.length-1];</pre><pre>      // Do what you want to do with the data, i.e.<br>      // append to page or dispatch({ … }) if using redux</pre><pre>   })<br>   .catch((error) =&gt; { … } );<br> <br> }</pre><p>Hopefully the article wasn’t complete waste of your time and you actually picked something up from it :).</p><p>By the way, if you’re a Twitter person you can reach me there at <a href="https://twitter.com/linasmnew">linasmnew</a>, <em>(I’m new there</em> 😅 😄)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e28bfbc53578" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/infinite-scrolling-in-firebase-e28bfbc53578">Infinite Scrolling In Firebase</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>