<?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: Martinez</title>
    <description>The latest articles on DEV Community by Martinez (@martinez).</description>
    <link>https://dev.to/martinez</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%2F1017825%2F47879a45-5cea-4824-ae9a-7d648cb12cb5.jpeg</url>
      <title>DEV Community: Martinez</title>
      <link>https://dev.to/martinez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/martinez"/>
    <language>en</language>
    <item>
      <title>Real Scroll for a Chat Application</title>
      <dc:creator>Martinez</dc:creator>
      <pubDate>Thu, 02 Feb 2023 04:18:28 +0000</pubDate>
      <link>https://dev.to/martinez/real-scroll-for-a-chat-application-22co</link>
      <guid>https://dev.to/martinez/real-scroll-for-a-chat-application-22co</guid>
      <description>&lt;p&gt;One of the challenges when creating a chat application is efficient scroll control, especially as new messages continue to arrive and older messages can get lost in the message container.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyn260f754q1du005w8l9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyn260f754q1du005w8l9.gif" alt="Twitch chat" width="1024" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the chat app world, scroll is a critical feature that affects the user experience.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this post, we will learn how to create an efficient scroll in a chat application using &lt;code&gt;Next.js / React&lt;/code&gt;. The logic can also be applied to &lt;code&gt;Vanilla JavaScript&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Just Show Me the Code
&lt;/h2&gt;

&lt;p&gt;Without further ado, here’s a scroll setup that handles both new and old messages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="err"&gt;useState,&lt;/span&gt; &lt;span class="err"&gt;useEffect,&lt;/span&gt; &lt;span class="err"&gt;useRef&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nt"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;'react'&lt;/span&gt;

&lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="nt"&gt;Chat&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;const&lt;/span&gt; &lt;span class="err"&gt;[messages,&lt;/span&gt; &lt;span class="err"&gt;setMessages]&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;useState&amp;lt;string[]&amp;gt;([])&lt;/span&gt;
  &lt;span class="err"&gt;const&lt;/span&gt; &lt;span class="err"&gt;container&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;useRef&amp;lt;HTMLDivElement&amp;gt;(null)&lt;/span&gt;

  &lt;span class="err"&gt;const&lt;/span&gt; &lt;span class="err"&gt;Scroll&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;()&lt;/span&gt; &lt;span class="err"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;const&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt; &lt;span class="err"&gt;offsetHeight,&lt;/span&gt; &lt;span class="err"&gt;scrollHeight,&lt;/span&gt; &lt;span class="err"&gt;scrollTop&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nt"&gt;container&lt;/span&gt;&lt;span class="nc"&gt;.current&lt;/span&gt; &lt;span class="nt"&gt;as&lt;/span&gt; &lt;span class="nt"&gt;HTMLDivElement&lt;/span&gt;
    &lt;span class="nt"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;scrollHeight&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nt"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;offsetHeight&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="err"&gt;100&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="err"&gt;container.current?.scrollTo(0,&lt;/span&gt; &lt;span class="err"&gt;scrollHeight)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="err"&gt;}&lt;/span&gt;

  &lt;span class="nt"&gt;useEffect&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;Scroll()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;

  &lt;span class="nt"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="nt"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;container&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Feel free to copy paste it in your project.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you don’t care how this works, you can stop here! The rest of this post is for those who want to understand the logic behind the code.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Extraction
&lt;/h2&gt;

&lt;p&gt;Here I show you how to efficiently control the scroll in just 3 simple steps. It's actually quite simple.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Identify the message container.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This component is simple and only has the function of displaying messages. However, In complex apps, more styling is needed to make messages visually appealing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Get a reference to this element.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These properties must be mutable and must “persist” in new renderings, so it can't be a regular variable. We want something more like an instance field.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useRef()&lt;/code&gt; gives us exactly that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Hooks use a similar concept to store any value that can change. A reference is like a container where you can store anything.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;useRef()&lt;/code&gt; is a hook in React that allows you to access and manipulate a value that persists across multiple renders. It is ideal for storing properties, precisely what we need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLDivElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Access the relevant properties to control the scroll.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before going deeper into the implementation, let's briefly describe the use of each property we are going to use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The height of the element &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight" rel="noopener noreferrer"&gt;scrollHeight&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The number of pixels that the content of the element has been vertically displaced &lt;a href="https://developer.mozilla.org/es/docs/Web/API/Element/scrollTop" rel="noopener noreferrer"&gt;scrollTop&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The height of the element, including vertical padding and borders &lt;a href="https://developer.mozilla.org/es/docs/Web/API/HTMLElement/offsetHeight" rel="noopener noreferrer"&gt;offsetHeight&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Moves the viewer to a specific set of coordinates on the element &lt;a href="https://developer.mozilla.org/es/docs/Web/API/Window/scrollTo" rel="noopener noreferrer"&gt;scrollTo&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's start by creating a function, which will be responsible for handling the scrolling logic in our chat application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;const&lt;/span&gt; &lt;span class="nt"&gt;Scroll&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Scroll()&lt;/code&gt; function checks the current scroll position of the element referenced by the &lt;code&gt;container&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;In the following, we will use destructuring to access three of its important properties: &lt;code&gt;offsetHeight&lt;/code&gt;, &lt;code&gt;scrollHeight&lt;/code&gt; y &lt;code&gt;scrollTop&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="err"&gt;offsetHeight,&lt;/span&gt; &lt;span class="err"&gt;scrollHeight,&lt;/span&gt; &lt;span class="err"&gt;scrollTop&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nt"&gt;container&lt;/span&gt;&lt;span class="nc"&gt;.current&lt;/span&gt; &lt;span class="nt"&gt;as&lt;/span&gt; &lt;span class="nt"&gt;HTMLDivElement&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we do a comparison to determine if the user has reached the end of the content with a margin of &lt;code&gt;100 pixels&lt;/code&gt;. If this condition is met, we use the &lt;code&gt;scrollTo&lt;/code&gt; method to automatically scroll to the end of the content and view the last messages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;scrollHeight&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nt"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;offsetHeight&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="err"&gt;100&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;container.current?.scrollTo(0,&lt;/span&gt; &lt;span class="err"&gt;scrollHeight)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;But when is it necessary to activate the function? 🤔&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To do this, we use the &lt;code&gt;useEffect&lt;/code&gt; hook of React, which allows us to execute a function at a specific time in the lifecycle of our application. In this case, we are saying that the &lt;code&gt;Scroll()&lt;/code&gt; function should be executed every time the status of the &lt;code&gt;messages&lt;/code&gt; in our chat application changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;useEffect&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;Scroll()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks to &lt;code&gt;[messages]&lt;/code&gt;, our effect is re-executed each time we receive a new message. In this way, we ensure that the Scroll function is executed each time a new message is added to the conversation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Closing Thoughts
&lt;/h2&gt;

&lt;p&gt;After adding the &lt;code&gt;Scroll()&lt;/code&gt; feature to your chat application, it is important to remember that this is only a basic example. In more complex applications, more formatting and styling will probably be needed to make messages appealing to users, and additional features such as scroll pause or message search can also be added. Overall, scrolling logic is only one part of building a complete chat application and there is much more to do before it is ready for use.&lt;/p&gt;

</description>
      <category>offers</category>
      <category>cryptocurrency</category>
      <category>crypto</category>
      <category>web3</category>
    </item>
  </channel>
</rss>
