<?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: Valentina</title>
    <description>The latest articles on DEV Community by Valentina (@vale).</description>
    <link>https://dev.to/vale</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%2F241716%2Fe669be37-3faf-49a8-a0b1-6d2bc64bb389.jpeg</url>
      <title>DEV Community: Valentina</title>
      <link>https://dev.to/vale</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vale"/>
    <language>en</language>
    <item>
      <title>Unleashing the Power of Server Components in React.js</title>
      <dc:creator>Valentina</dc:creator>
      <pubDate>Sun, 30 Jul 2023 12:49:49 +0000</pubDate>
      <link>https://dev.to/vale/unleashing-the-power-of-server-components-in-reactjs-kn4</link>
      <guid>https://dev.to/vale/unleashing-the-power-of-server-components-in-reactjs-kn4</guid>
      <description>&lt;p&gt;React.js has always been a frontrunner in the world of front-end development due to its declarative style and efficient data-updating mechanisms. &lt;/p&gt;

&lt;p&gt;One of the more recent updates to this powerful library is the introduction of &lt;strong&gt;Server Components&lt;/strong&gt;, marking another milestone in the evolution of React. &lt;/p&gt;

&lt;p&gt;In this article, we'll explore the intriguing world of Server Components in React.js, understand their uniqueness, and delve into some compelling reasons why developers should embrace this innovative feature.&lt;/p&gt;

&lt;h2&gt;
  
  
  An Introduction to Server Components in React.js
&lt;/h2&gt;

&lt;p&gt;React Server Components, in essence, let you create components that are rendered on the server and sent to the client as HTML. The beauty of these components lies in their ability to handle data-intensive operations on the server-side, thereby significantly reducing the data sent to the client and improving the overall performance of your application.&lt;/p&gt;

&lt;p&gt;But what does that really mean for developers and end-users? &lt;/p&gt;

&lt;p&gt;Imagine a feature in your application that involves some heavy data-fetching and manipulation operations. With client-side rendering, all the heavy lifting happens on the client's device, leading to slower load times and higher data usage. But when you use a Server Component to manage this feature, all the intensive computations are done on the server, which translates to less JavaScript being shipped to the client, faster load times, and smoother user experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Harnessing the Power of Server Components
&lt;/h2&gt;

&lt;p&gt;Understanding how to leverage Server Components requires an appreciation for their unique characteristics. Here are some examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  An Intense News Aggregator - Using Server Components
&lt;/h3&gt;

&lt;p&gt;Let's assume you're building a news aggregator that fetches a large number of articles from various sources. Rather than having the client's device handle the fetching, parsing, and displaying of this data, you could use Server Components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./db.server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;NewsFeed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;articles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SELECT * FROM articles LIMIT 100&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;articles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;article&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Article&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;article&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;article&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;NewsFeed&lt;/code&gt; Server Component fetches and renders the articles on the server. By doing this, we avoid sending a large amount of JavaScript to the client, making the app more performant.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Real Estate Platform - Server Components with Zero Client-side JS
&lt;/h3&gt;

&lt;p&gt;Imagine you're developing a real estate platform that includes property listings with images, descriptions, and other details. The server can handle fetching and rendering these details, drastically reducing the amount of client-side JavaScript required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./db.server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;PropertyDetails&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;property&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SELECT * FROM properties WHERE id = ?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;alt&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;property&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;PropertyDetails&lt;/code&gt; is a Server Component that handles the data-fetching and rendering, reducing the load on the client's device and offering an enhanced user experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Avoiding Pitfalls with Server Components
&lt;/h2&gt;

&lt;p&gt;While Server Components offer impressive advantages, they may not be the right fit for every scenario. Here's a case where their use might not be optimal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Misuse in Real-time Updates
&lt;/h3&gt;

&lt;p&gt;Server Components are not ideal for parts of your application that need real-time updates. For instance, if you're building a chat application where the UI needs to be updated instantaneously when new messages arrive, using a Server Component for the chat interface would be counterproductive.&lt;/p&gt;

&lt;p&gt;The server doesn't have a persistent connection to the client, so it cannot push updates. Instead, using a Client Component with a real-time data solution like WebSockets would be more suitable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;React's Server Components represent a leap forward in creating data-intensive, high-performance applications. They allow developers to offload heavy computations to the server, resulting in leaner, faster client-side experiences.&lt;/p&gt;

&lt;p&gt;However, like any technology, they come with their own caveats. Understanding when and how to use Server Components is crucial to leverage their benefits without running into pitfalls. By effectively integrating Server Components into your React toolkit, you can take your applications to new heights of performance and efficiency.&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>performance</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Decoding the Magic of React’s useEffect Hook</title>
      <dc:creator>Valentina</dc:creator>
      <pubDate>Sun, 30 Jul 2023 11:33:59 +0000</pubDate>
      <link>https://dev.to/vale/decoding-the-magic-of-reacts-useeffect-hook-1f0h</link>
      <guid>https://dev.to/vale/decoding-the-magic-of-reacts-useeffect-hook-1f0h</guid>
      <description>&lt;p&gt;React has made a profound impact on how we approach front-end development, offering an elegant, declarative, and efficient way to build user interfaces. &lt;/p&gt;

&lt;p&gt;Since the introduction of Hooks in version 16.8, the landscape has shifted significantly, and developers have been empowered with a fresh, innovative way to handle side effects in functional components.&lt;/p&gt;

&lt;p&gt;One hook that has become indispensable is the &lt;code&gt;useEffect&lt;/code&gt; hook. &lt;/p&gt;

&lt;p&gt;However, its true potential is often misunderstood or underutilized. &lt;/p&gt;

&lt;p&gt;In this article, we will reveal the magic behind &lt;code&gt;useEffect&lt;/code&gt;, discover less-known facets of its behavior, and learn &lt;strong&gt;when&lt;/strong&gt;, &lt;strong&gt;why&lt;/strong&gt;, and &lt;strong&gt;how&lt;/strong&gt; to use it most effectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Unveiling of useEffect
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useEffect&lt;/code&gt; hook is designed to handle side effects in functional components, allowing us to perform actions in response to component lifecycle events like mounting, updating, and unmounting.&lt;/p&gt;

&lt;p&gt;It takes two arguments: a function where you can place your side-effect logic and a dependency array. The function runs after the render, and it runs again if any dependencies in the dependency array change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mastering the Art of Using useEffect
&lt;/h2&gt;

&lt;p&gt;Understanding the basics is good, but we'll now explore some more advanced concepts and situations where &lt;code&gt;useEffect&lt;/code&gt; can be a real game-changer.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Chat App - Using useEffect for Subscriptions
&lt;/h3&gt;

&lt;p&gt;Let's say you're building a chat application, where users need to subscribe to a chat room when they enter and unsubscribe when they leave. Using class components, this logic would be split across different lifecycle methods. However, with &lt;code&gt;useEffect&lt;/code&gt;, we can have all the related logic in one place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ChatRoom&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;chatRoomId&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&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="nx"&gt;setMessages&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleNewMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setMessages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevMessages&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;prevMessages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;subscribeToChatRoom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chatRoomId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handleNewMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return &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="nf"&gt;unsubscribeFromChatRoom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chatRoomId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handleNewMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;chatRoomId&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this scenario, when the &lt;code&gt;chatRoomId&lt;/code&gt; changes, &lt;code&gt;useEffect&lt;/code&gt; cleans up by running the return function, unsubscribing from the old chat room. It then runs the effect again, subscribing to the new chat room.&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance Monitoring - Using useEffect for Tracking
&lt;/h3&gt;

&lt;p&gt;Imagine that you want to track user behavior in your app, such as how long users spend on a specific page. &lt;code&gt;useEffect&lt;/code&gt; allows us to implement this in a clean and efficient way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ProductPage&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;productId&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;startTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;return &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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;endTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;duration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;endTime&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;startTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nf"&gt;trackUserBehavior&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;productPageViewDuration&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the component mounts, it captures the current time. And when the component unmounts, it calculates the duration of the user's visit and logs it.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Not to Use useEffect
&lt;/h2&gt;

&lt;p&gt;Just as with any other tool, misuse or overuse of &lt;code&gt;useEffect&lt;/code&gt; can lead to performance issues and difficult-to-track bugs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Overdoing Dependency Arrays
&lt;/h3&gt;

&lt;p&gt;Remember, each time a dependency changes, &lt;code&gt;useEffect&lt;/code&gt; runs the cleanup function (if provided) and then the effect function. If you include items that change too frequently or unnecessarily in the dependency array, you could cause unnecessary work and potential performance degradation. Always ensure you only include necessary dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ignoring Cleanup
&lt;/h3&gt;

&lt;p&gt;When using &lt;code&gt;useEffect&lt;/code&gt; for subscriptions, event listeners, or other side effects that require cleanup, ignoring or forgetting the cleanup function can lead to memory leaks and unexpected behavior. Always ensure to provide a cleanup function where necessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding &lt;code&gt;useEffect&lt;/code&gt; is fundamental to mastering React Hooks. This hook allows us to encapsulate side-effect logic within our functional components, leading to more readable and maintainable code.&lt;/p&gt;

&lt;p&gt;However, careful usage is necessary to prevent unnecessary re-renders and other potential performance issues. By truly understanding and effectively using &lt;code&gt;useEffect&lt;/code&gt;, we can build highly efficient and resilient React applications that are easier to reason about and maintain.&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Understanding the Differences Between useCallback and useMemo</title>
      <dc:creator>Valentina</dc:creator>
      <pubDate>Wed, 26 Jul 2023 19:34:07 +0000</pubDate>
      <link>https://dev.to/vale/understanding-the-differences-between-usecallback-and-usememo-il5</link>
      <guid>https://dev.to/vale/understanding-the-differences-between-usecallback-and-usememo-il5</guid>
      <description>&lt;p&gt;React has revolutionized the way we build interfaces with its innovative and dynamic approach. &lt;/p&gt;

&lt;p&gt;Ever since version 16.8 rolled out, Hooks have become a game-changer, enabling developers to work with state and other React features without the need for classes. &lt;/p&gt;

&lt;p&gt;Among these Hooks, two stand out for their significance: &lt;code&gt;useCallback&lt;/code&gt; and &lt;code&gt;useMemo&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;In this article, we'll take a deep dive into these hooks, understand their differences, and learn &lt;strong&gt;when&lt;/strong&gt;, &lt;strong&gt;why&lt;/strong&gt;, and &lt;strong&gt;how&lt;/strong&gt; they should (or should &lt;em&gt;not&lt;/em&gt;) be used.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Brief Introduction to useCallback and useMemo
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useCallback&lt;/code&gt; hook returns a memoized version of the callback function that only changes if one of the dependencies has changed. It's useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useMemo&lt;/code&gt; hook returns a memoized value. Like &lt;code&gt;useCallback&lt;/code&gt;, it only re-calculates the memoized value when one of the dependencies has changed. It's great for expensive calculations.&lt;/p&gt;

&lt;h2&gt;
  
  
  When and How to Use useCallback and useMemo
&lt;/h2&gt;

&lt;p&gt;Now that we have a brief understanding, let's jump into some practical examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  An Intricate Game Scenario - Using useCallback
&lt;/h3&gt;

&lt;p&gt;Imagine we're creating an intricate game application where a player can increase their level by collecting gems. Every time they level up, they get a celebratory message. Here's a simplified version of that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Game&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;level&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLevel&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;levelUp&lt;/span&gt; &lt;span class="o"&gt;=&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="nf"&gt;setLevel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;level&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Board&lt;/span&gt; &lt;span class="na"&gt;level&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;level&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;onLevelUp&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;levelUp&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Board&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;level&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onLevelUp&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// A heavy computation function that renders the game board&lt;/span&gt;
  &lt;span class="nf"&gt;renderBoard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;level&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Current Level: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;level&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onLevelUp&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Collect a gem&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the issue here is that the &lt;code&gt;levelUp&lt;/code&gt; function is created each time &lt;code&gt;Game&lt;/code&gt; renders. Thus, &lt;code&gt;Board&lt;/code&gt; re-renders every time, even when there are no level changes. This might slow down our app, especially with complex board rendering. Here's where &lt;code&gt;useCallback&lt;/code&gt; shines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Game&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;level&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLevel&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;levelUp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&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="nf"&gt;setLevel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;level&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;level&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Board&lt;/span&gt; &lt;span class="na"&gt;level&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;level&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;onLevelUp&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;levelUp&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&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;With this change, a memoized &lt;code&gt;levelUp&lt;/code&gt; function is passed to &lt;code&gt;Board&lt;/code&gt; unless the &lt;code&gt;level&lt;/code&gt; changes. The expensive board rendering process only happens when necessary, improving performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  An E-Commerce Filter - Using useMemo
&lt;/h3&gt;

&lt;p&gt;Suppose you're building an e-commerce app with a product listing page. There's a filter feature that allows users to search for products by their names. Here's a basic setup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ProductList&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFilter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filteredProducts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
        &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setFilter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;filteredProducts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&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 issue is, the &lt;code&gt;filter&lt;/code&gt; function runs each time &lt;code&gt;ProductList&lt;/code&gt; renders. If you have thousands of products, this could slow down your app significantly. The &lt;code&gt;useMemo&lt;/code&gt; hook can solve this problem:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ProductList&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFilter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filteredProducts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
    &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the expensive filter function only runs when &lt;code&gt;products&lt;/code&gt; or &lt;code&gt;filter&lt;/code&gt; changes, leading to much better performance for your product list.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Not to Use useCallback and useMemo
&lt;/h2&gt;

&lt;p&gt;While &lt;code&gt;useCallback&lt;/code&gt; and &lt;code&gt;useMemo&lt;/code&gt; can provide performance boosts in specific scenarios, they should not be used everywhere. Here's why.&lt;/p&gt;

&lt;h3&gt;
  
  
  Overuse of useCallback
&lt;/h3&gt;

&lt;p&gt;Using &lt;code&gt;useCallback&lt;/code&gt; unnecessarily can lead to more harm than good. It creates an overhead of maintaining the memoized version of the function, which can be more expensive than the function invocation itself. Let's consider an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Greeting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updateName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;event&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="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
      &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;updateName&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;useCallback&lt;/code&gt; is not needed, because the &lt;code&gt;updateName&lt;/code&gt; function is not computationally expensive or passed as a prop causing unnecessary re-renders. Removing &lt;code&gt;useCallback&lt;/code&gt; from this code simplifies it without any performance downside.&lt;/p&gt;

&lt;h3&gt;
  
  
  Misuse of useMemo
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;useMemo&lt;/code&gt; can also be overused or misused. If the calculation isn't computationally expensive, then &lt;code&gt;useMemo&lt;/code&gt; might bring more overhead than benefits. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;TotalPrice&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;totalPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;quantity&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Total Price: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;totalPrice&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;useMemo&lt;/code&gt; is unnecessary since the multiplication operation isn't expensive. It would be better to simply calculate &lt;code&gt;totalPrice&lt;/code&gt; without memoization:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;TotalPrice&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;totalPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;quantity&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Total Price: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;totalPrice&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useCallback&lt;/code&gt; and &lt;code&gt;useMemo&lt;/code&gt; are powerful tools in the React developer's toolkit. They are designed to help optimize React apps by preventing unnecessary renders and computations. &lt;/p&gt;

&lt;p&gt;However, like many powerful tools, they can lead to issues when used inappropriately or excessively. By understanding the use cases for these hooks and using them judiciously, we can create more performant and maintainable React applications.&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Let's Make Arrays Dance: 4 Approaches to Rotate an Array in JavaScript</title>
      <dc:creator>Valentina</dc:creator>
      <pubDate>Wed, 19 Jul 2023 11:59:37 +0000</pubDate>
      <link>https://dev.to/vale/lets-make-arrays-dance-4-approaches-to-rotate-an-array-in-javascript-2jb6</link>
      <guid>https://dev.to/vale/lets-make-arrays-dance-4-approaches-to-rotate-an-array-in-javascript-2jb6</guid>
      <description>&lt;h1&gt;
  
  
  Let's Make Arrays Dance: 4 Approaches to Rotate an Array in JavaScript
&lt;/h1&gt;

&lt;p&gt;One frequently encountered task in programming interviews and problem-solving is the rotation of an array. The versatility of JavaScript as a language presents us with many intriguing ways to go about this. Let's navigate through these various approaches.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Straightforward Shuffle Using Splice and Concat
&lt;/h2&gt;

&lt;p&gt;As you venture into the realm of JavaScript, one simple and straightforward way to address this task awaits you: using the &lt;code&gt;splice()&lt;/code&gt; and &lt;code&gt;concat()&lt;/code&gt; methods. It's undeniably effective!&lt;/p&gt;

&lt;p&gt;Here's how it looks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;rotateArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's dissect how this works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;splice()&lt;/code&gt; method extracts the initial elements of our array, the number of which is determined by our variable &lt;code&gt;steps&lt;/code&gt;. These elements are removed from the original array.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;concat()&lt;/code&gt; method appends the extracted elements to the end of the remaining elements in the array.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And just like that, we have our rotated array! 😉&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Word of caution: This method is suitable when you are explicitly permitted to use built-in JavaScript functions. If this isn't the case, stick around for the other techniques we're about to discuss.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. The Classic Spin: Looping through the Array
&lt;/h2&gt;

&lt;p&gt;To flex your core JavaScript knowledge during an interview, you might want to lean on the traditional for loop.&lt;/p&gt;

&lt;p&gt;How about spicing it up a little with ES6 syntax? Sounds good, right?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;rotateArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&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 modern &lt;code&gt;for loop&lt;/code&gt; syntax introduced by ES6 is both user-friendly and less error-prone. Plus, it just looks so sleek! 👌&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The Trendy Twist: Array Destructuring
&lt;/h2&gt;

&lt;p&gt;ES6 brought another nifty feature into JavaScript: array destructuring, and it fits perfectly into our current narrative.&lt;/p&gt;

&lt;p&gt;This approach is akin to the first method we discussed, but with a modern twist. Here's how it goes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;rotateArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;shiftArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;shiftArray&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool, right? 😎&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;splice()&lt;/code&gt; method extracts the elements from our original array. The spread operator &lt;code&gt;...&lt;/code&gt; is then used to destructure these elements and concatenate them with the remaining elements in the array.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. And Finally... The Recursive Rotation!
&lt;/h2&gt;

&lt;p&gt;The last method I'm excited to share is a recursive approach to our array rotation problem.&lt;/p&gt;

&lt;p&gt;This technique uses the function to call itself recursively until we reach our base case, which in this instance is when the &lt;code&gt;steps&lt;/code&gt; variable reaches zero.&lt;/p&gt;

&lt;p&gt;We can even use the ternary operator to make our JavaScript syntax more concise and clean.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;rotateArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;steps&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;rotateArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt; &lt;span class="nx"&gt;steps&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that concludes our dance with array rotation using JavaScript! 🎉&lt;/p&gt;

&lt;p&gt;I sincerely hope you found this tutorial insightful. Do share your thoughts and your own unique solutions too!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>array</category>
    </item>
    <item>
      <title>Design Principles &amp; why they matter (a lot)</title>
      <dc:creator>Valentina</dc:creator>
      <pubDate>Tue, 01 Jun 2021 11:48:19 +0000</pubDate>
      <link>https://dev.to/vale/design-principles-why-they-matter-a-lot-368n</link>
      <guid>https://dev.to/vale/design-principles-why-they-matter-a-lot-368n</guid>
      <description>&lt;p&gt;Design Principles are one of those key ingredients that shape the actual perceived usability &amp;amp; vision of a certain product under the hood, and that at a first glance could almost appear as the output of a bunch of meaningless marketing jargon but whose real purpose goes so far beyond that.&lt;/p&gt;

&lt;p&gt;I was initially able to reflect on the understated importance of Design Principles during my &lt;a href="https://mailchimp.com/culture/what-sets-our-internship-program-apart/" rel="noopener noreferrer"&gt;internship with Mailchimp&lt;/a&gt; in Atlanta about two years ago, which marked a significant point in my career as a Front-End Engineer.&lt;/p&gt;

&lt;p&gt;As a matter of fact, being able to see how such an established company thinks about its product &amp;amp; the Design aspect connect to it is absolutely fascinating, and I gotta say that I truly learned a lot from that experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Design Principles&lt;/strong&gt; encompass the whole philosophy that defines &amp;amp; surrounds a certain product, they act as a framework for decision making, they become collaborative tools to bridge the communication gaps with other teams &amp;amp; stakeholders, and ultimately shape the whole look &amp;amp; feel of the product with their values. &lt;/p&gt;

&lt;p&gt;They also help to establish a common vocabulary across different teams/squads, which can serve as a helpful tool during internal product meetings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A great example of Design Principles&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's take a nice example of great design principles and analyse them, so to better understand their intrinsic purpose and value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://spotify.com" rel="noopener noreferrer"&gt;Spotify&lt;/a&gt; is one of those companies that deeply care about building amazing UX for their end-users, and their &lt;a href="https://spotify.design/article/introducing-spotifys-new-design-principles" rel="noopener noreferrer"&gt;guiding principles&lt;/a&gt; are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Content First&lt;/li&gt;
&lt;li&gt;Be Alive&lt;/li&gt;
&lt;li&gt;Get Familiar&lt;/li&gt;
&lt;li&gt;Do Less&lt;/li&gt;
&lt;li&gt;Stay Authentic&lt;/li&gt;
&lt;li&gt;Lagom&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These principles are so specific and precise in their intent, and they clearly communicate what Spotify values in a User Experience.&lt;/p&gt;

&lt;p&gt;They tell a lot about the way Spotify thinks about product, how they want to present content, personalisation, not over-engineering and being very intentional about what they’re putting forward.&lt;/p&gt;

&lt;p&gt;The last principle &lt;em&gt;"Lagom"&lt;/em&gt; is actually a cool Swedish word for "just enough", which is like a common way of life to indicate the need for the perfect balance when it comes to things.&lt;/p&gt;

&lt;p&gt;If we were to point out what makes for great Design Principles we could confidently borrow Alla Kholmatova's principles as an interesting starting point &lt;em&gt;(they're included in her &lt;a href="https://www.amazon.com/Design-Systems-practical-creating-languages/dp/3945749581" rel="noopener noreferrer"&gt;book&lt;/a&gt; "Design Systems. A Practical Guide To Creating Design Languages for Digital Products").&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Great Design Principles&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They're specific.&lt;/li&gt;
&lt;li&gt;They're actionable.&lt;/li&gt;
&lt;li&gt;They're impressionable.&lt;/li&gt;
&lt;li&gt;They have a point of view.&lt;/li&gt;
&lt;li&gt;They're living.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Specificity&lt;/strong&gt; is probably the most important aspect to keep in mind among those 5 principles when it comes to conceptualising new Design Principles, as having a clear picture in your mind about what the brand wants to convey to its end-users and preserving the unicity of the core values is vital.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actionable&lt;/strong&gt; refers to the fact that new Design Principles need to be concrete and practical in their intent so that you can effectively act upon them to make a real impact.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Impressionable&lt;/strong&gt; because a design principle needs to have a soul, a well-defined motif that gets stuck in your head whenever you think about it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;They have a point of view&lt;/strong&gt; because of course they need to be opinionated somehow, otherwise they wouldn't be able to properly reflect the brand values and would defeat their own effective purpose.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;They're living&lt;/strong&gt; because they need to be flexible enough in order to accommodate any new tension points and discoveries related to the brand. They have the capacity to evolve over time and renew themselves.&lt;/p&gt;

&lt;p&gt;What about &lt;em&gt;you&lt;/em&gt;? Have you ever taken part in the brainstorming process for new Design Principles in your company? How did you decide what was important in shaping those principles? &lt;/p&gt;

&lt;p&gt;Please feel free to share your experience &amp;amp; thoughts about this topic down in the comments!&lt;/p&gt;

</description>
      <category>ux</category>
      <category>design</category>
      <category>frontend</category>
      <category>usability</category>
    </item>
    <item>
      <title>4 different ways to reverse a string with JavaScript</title>
      <dc:creator>Valentina</dc:creator>
      <pubDate>Thu, 11 Jun 2020 18:52:24 +0000</pubDate>
      <link>https://dev.to/vale/4-different-ways-to-reverse-a-string-with-javascript-34cl</link>
      <guid>https://dev.to/vale/4-different-ways-to-reverse-a-string-with-javascript-34cl</guid>
      <description>&lt;p&gt;Reversing a string is perhaps one of the most common interview questions one of those which inevitably we, as Software Developers, encounter during our journeys.&lt;/p&gt;

&lt;p&gt;When it comes to JavaScript and its many quirkinesses, one might feel overwhelmed by the many possible ways to go about tackling this particular problem, and here I would like to show you a few viable approaches.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The quick &amp;amp; dirty way using built-in methods
&lt;/h2&gt;

&lt;p&gt;This is probably the most common and dirty way to approach this problem with JavaScript…but it does the job!&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%2Fi%2F2xqqmhe5yk14cjv54beg.png" 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%2Fi%2F2xqqmhe5yk14cjv54beg.png" alt="Alt Text" width="800" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s briefly recap how this approach actually works.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;split()&lt;/code&gt; method splits our String object, so that every single character in the original String array is now separated in its own single-character substring.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;reverse()&lt;/code&gt; method comes then into play and does the main job: it simply reverses the elements contained in the originally array so that what originally was the last character in the -String array, is now the first element (in our case it’s a “.” , since it’s the character that our variable “stringToReverse” was ending on).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;join()&lt;/code&gt; method finally rejoins all the single characters previously separated by the &lt;code&gt;split()&lt;/code&gt; method to form and recompose our reversed string.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Et voilà! 😏&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Quick side note : This approach should preferably be used if your interviewer specifically says that you are allowed to use built-in methods, otherwise you should go for the next approaches I am going to show you.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. The Old Fashioned Way: the for loop
&lt;/h2&gt;

&lt;p&gt;If you wanna show your interviewer that you know how to solve this problem without relying on the JS built-in methods, just use the for loop.&lt;/p&gt;

&lt;p&gt;But let’s just get a bit fancier and use the ES6 syntax, shall we?&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%2Fi%2Fd41t3jsuzpsw2o4rkz7o.png" 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%2Fi%2Fd41t3jsuzpsw2o4rkz7o.png" alt="Alt Text" width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The new &lt;code&gt;for loop&lt;/code&gt; syntax introduced by ES6 is very handy and it drastically reduces the chance of making mistakes while typing code for the loop.&lt;br&gt;
Also, it is much cleaner and nicer to look at 👌 .&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The Concise and even more “ES6” way of doing things with the Spread Syntax
&lt;/h2&gt;

&lt;p&gt;With ES6 we have a new way of splitting our String into an array, thanks to the so-called spread operator &lt;code&gt;[…]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This approach is almost identical to the first approach I showed (except for the &lt;code&gt;[…]&lt;/code&gt; operator of course 😅 ) and here below you can have a look at it.&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%2Fi%2Fgwvxwg0ur663xqp8fqs6.png" 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%2Fi%2Fgwvxwg0ur663xqp8fqs6.png" alt="Alt Text" width="800" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty neat, huh? 😎&lt;/p&gt;

&lt;p&gt;Passing the “stringToReverse” as a parameter of the spread operator allows us to “spread” the single character values contained in our original array to get the same result as we did using the &lt;code&gt;split()&lt;/code&gt; function, so that we can later reverse the all the single characters one by one as we did before with the JS built-in method &lt;code&gt;reverse()&lt;/code&gt; and then finish off once more with the &lt;code&gt;join()&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Last But Not Least….Recursion!
&lt;/h2&gt;

&lt;p&gt;The last method I would like to show you is the recursive approach to this problem.&lt;/p&gt;

&lt;p&gt;In this case we want to set up our function so that it recursively calls itself till it hits our base case (i.e. an empty string).&lt;/p&gt;

&lt;p&gt;We simply cut the first character of our string object away with the &lt;code&gt;substr()&lt;/code&gt; method, and add it at the end of the string recursively till there are no more characters left to add.&lt;/p&gt;

&lt;p&gt;We can even do this with a ternary operator to be more concise and clean with our JS syntax.&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%2Ftszqiyyljy2y40oaer3k.png" 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%2Ftszqiyyljy2y40oaer3k.png" alt="Alt Text" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that was the last example on how to reverse a string with JavaScript! 🎉&lt;/p&gt;

&lt;p&gt;I truly hope you enjoyed this tutorial and please let me know your thoughts and solutions too!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>interview</category>
      <category>question</category>
      <category>string</category>
    </item>
  </channel>
</rss>
