<?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>Forem: Sibusiso Massango</title>
    <description>The latest articles on Forem by Sibusiso Massango (@samassango).</description>
    <link>https://forem.com/samassango</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%2F631025%2Febfb0799-862d-4ff7-a8cc-3adecf2a4ae9.png</url>
      <title>Forem: Sibusiso Massango</title>
      <link>https://forem.com/samassango</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/samassango"/>
    <language>en</language>
    <item>
      <title>JavaScript Array checks</title>
      <dc:creator>Sibusiso Massango</dc:creator>
      <pubDate>Fri, 23 Feb 2024 09:44:34 +0000</pubDate>
      <link>https://forem.com/samassango/javascript-array-checks-4cje</link>
      <guid>https://forem.com/samassango/javascript-array-checks-4cje</guid>
      <description>&lt;p&gt;In JavaScript there are many ways to check the validity of an array, but so far, I haven't found a way which I will be proud of when checking if array has values. Below are ways we use to check whether array has values?&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Assume you don't know whether the array has values or not, but you want to perform some actions once it does.

let arr = [];

// this should at least tell us whether the array has values or not, but it still assumes that the variable is an array. 

if(arr.length &amp;gt; 0){
}
// What if is not, then below is a full proof example to prevent this.
if(arr &amp;amp;&amp;amp; arr.length &amp;gt; 0){
}
// This will check for null and undefined, and then check if it's an array.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The question is there a better way to write the above condition in advance JS?&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>discuss</category>
    </item>
    <item>
      <title>How useMemo and useCallback hooks work?</title>
      <dc:creator>Sibusiso Massango</dc:creator>
      <pubDate>Wed, 21 Jun 2023 08:57:04 +0000</pubDate>
      <link>https://forem.com/samassango/how-usememo-and-usecallback-hooks-work-ko9</link>
      <guid>https://forem.com/samassango/how-usememo-and-usecallback-hooks-work-ko9</guid>
      <description>&lt;p&gt;The useMemo and useCallback hooks are almost the same but designed and developed for different reasons. The implementation is the same they both expect a function and array of dependencies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation example&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;useMemo(()=&amp;gt;{},[])&lt;/code&gt; &lt;br&gt;
&lt;code&gt;useCallback(()=&amp;gt;{},[])&lt;/code&gt;&lt;br&gt;
The above code do look similar but serves different purposes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difference between useMemo and useCallback&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useMemo calls it's function and return the memoized results while useCallback return it's memoized function when dependencies changes.&lt;/li&gt;
&lt;li&gt;useMemo only compute when there was a change in dependencies and return memoized results.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What is Memoization?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Memoization&lt;/strong&gt; in javascript is an optimisation technique, to reduce the complexity of the application, runtime of the application, and proper utilisation of resources (Time and Memory).&lt;/p&gt;

&lt;p&gt;By understanding how to use this two hooks properly it will help improving your applications performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does useMemo() work in react?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const memoizedValue = useMemo(function,dependencies)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;On an initial rendering useMemo invokes the &lt;strong&gt;function&lt;/strong&gt;, memoize the calculated result and then returns it to the component.&lt;br&gt;
If the there's no change on &lt;strong&gt;dependencies&lt;/strong&gt; during the next rendering, then useMemo will not invoke the function, but it will returns the memoize value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does useCallback() work in react?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const memoizedFunction = useMemo(function,dependencies)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;React useCallback Hook returns a memoized callback function. Think of memoization as caching a value so that it does not need to be recalculated. This allows us to isolate resource intensive functions so that they will not automatically run on every render, but only run when the was a change in dependencies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Understanding how this hooks should help developers to implement them in a correct way, to ensure applications are as optimised as possible. This will also help to avoid creating or running functions every render.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
