<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[MBGCode]]></title><description><![CDATA[MBGCode]]></description><link>https://blog.mawitgad.com</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1658858914998/qcGRrWitY.jpg</url><title>MBGCode</title><link>https://blog.mawitgad.com</link></image><generator>RSS for Node</generator><lastBuildDate>Fri, 01 May 2026 05:32:27 GMT</lastBuildDate><atom:link href="https://blog.mawitgad.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How I solved My problem of accessing my react app from my local environment on my phone.]]></title><description><![CDATA[🤓So Basically, there is this react app that I have been working on for a while, and everything was going well until I reached a point where I needed to debug using my mobile device to have a more native feel of what it'd look like on there.
😌To do ...]]></description><link>https://blog.mawitgad.com/how-i-solved-my-problem-of-accessing-my-react-app-from-my-local-environment-on-my-phone</link><guid isPermaLink="true">https://blog.mawitgad.com/how-i-solved-my-problem-of-accessing-my-react-app-from-my-local-environment-on-my-phone</guid><category><![CDATA[React]]></category><category><![CDATA[vite]]></category><category><![CDATA[networking]]></category><category><![CDATA[firewall]]></category><dc:creator><![CDATA[Mawit Gad]]></dc:creator><pubDate>Fri, 23 Jun 2023 08:29:51 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1687508428912/3944a116-cfb6-4250-bc87-8172512e84ee.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>🤓So Basically, there is this react app that I have been working on for a while, and everything was going well until I reached a point where I needed to debug using my mobile device to have a more native feel of what it'd look like on there.</p>
<p>😌To do this, I knew that I had to get the host and port values in order to access it from my phone's browser. so I checked my console to see what host I could use and saw that my app was hosted on http://0.0.0.0:5173 and http:/localhost:5173 (💁‍♂️yes it's built with Vite). but none of them looked like an IP address I could access from an external device(💁‍♂️basically to access a device's resources on a network, you need to request it through the device's public Ip address on the network. 0.0.0.0 and localhost are not public). Some of you might see something similar to http://192.164.43.40:5173 in your console, that is likely to be the address you should use.</p>
<p>🙃To get my public IP address I ran the ipconfig command on a new console screen and it showed me my PC's public IP address</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1687502376942/eba4196f-7b42-426b-b3ec-ba0d919980fd.png" alt class="image--center mx-auto" /></p>
<p>💁‍♂️so I copied the address and slapped the app's port at the end to have something of this form http://192.168.43.40:5173, and ran it on the browser in my mobile phone, and then waited for it to load... and ⏰waited... and waited...⏰ and a minute later the browser gave up and displayed a connection timed out error message🤦‍♂️😒.</p>
<p>😔well, I was not really happy but that was not a big deal,🤷‍♂️I am a dev and so am used to errors being slapped at my face like that, so I began to think what could be wrong? did I copy the IP address incorrectly? - No, did I copy the port number incorrectly? - No, did I use HTTPS instead of HTTP? - No. therefore the problem must be hidden somewhere else.🤔</p>
<p>🌈And so I thought and made some research after which it came to my attention that I needed to check my network profile settings, because if my network profile was set to "public", it wouldn't be discoverable by other devices in the network(🥸which I found pretty counter-intuitive by the way, I would love someone to help me explain why it's so) and in contrast, if my network profile was set to "private" it would be accessible by the other devices on the network. And so I checked my settings by:</p>
<ul>
<li><p>✅ opening the settings app on my Windows 11</p>
</li>
<li><p>✅ selecting Network &amp; internet (on the left nav bar)</p>
</li>
<li><p>✅ selecting Wi-Fi (on the right viewport)</p>
</li>
<li><p>✅ selecting &lt;my-wifi-name&gt; properties (&lt;my-wifi-name&gt; shall be replaced by your wifi connection name)</p>
</li>
</ul>
<p>🤷‍♂️and voila... there it was, my network profile was set to "public"</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1687503771944/9023a7b9-1d18-49b4-8ef5-70f3ceac17a4.png" alt class="image--center mx-auto" /></p>
<p>so all I had to do now is switch it from "public" to "private" right? I did, restarted my app, and by the way I was now proposed to access the app from https://localhost:5173 or https://192.168.43.40:5173 which was now a good sign right?😌</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1687504039067/f8bbaefe-156f-4c91-9186-34e0da88792c.png" alt class="image--center mx-auto" /></p>
<p>yea, I went on my mobile device, copied the URL containing the IP address as indicated above, and ran it in my browser. and ⏰waited... ⏰waited... and ⏰waited... and a minute later, my browser gave up again and showed me a connection timed-out message for the second time, oh gosh... I was bitten.😔</p>
<p>But - a real warrior hmmm I mean develop... I mean Enginee... - sorry, but you get the point🙊. a real programmer does not give up so easily. So I continued searching for what could be the thing that stops my phone from accessing my PC's resources through the network and after a few more moments of reflection, It came to my attention that, the firewall could be the issue here. so I went on to check that out by clicking on the "Configure firewall and security settings" below the network profile settings from earlier which led me to the app that manages my firewall settings, this might vary depending on what you use so I will not go into the details of how I went from here but it should be pretty straight forward, you m need to make a little bit of research for your specific situation.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1687504923433/20266265-938f-4148-b717-6e060e85386e.png" alt class="image--center mx-auto" /></p>
<p>🤓and so I made the check on my firewall settings, and it was on, and therefore was at the origin of my problems. At this point, all I needed to do was to disable the firewall, and try again, and I did.</p>
<p>I then went back to my mobile browser and ran the URL again. and waited, and w.. wait it worked! it wooorked!!!🥳🥳🥹🥹</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>🛣️And that's my journey on how I solved my phone not accessing my React app from my PC. The moral lesson here is that for every problem, there always is a logical solution, as long as you don't give up, you'll find it.<br />That said, see you in the next article, and until then, Happy coding!😉</p>
<p><img src="https://media0.giphy.com/media/KfwyWfTwMu1FG0XhO8/giphy.gif?cid=ecf05e470t785dpjn1bw5dpw2ev9zl4l6ntuaovdgf4tqr4c&amp;ep=v1_gifs_search&amp;rid=giphy.gif&amp;ct=g" alt="happy time GIF by PRTG" /></p>
]]></content:encoded></item><item><title><![CDATA[A general overview of the DNS]]></title><description><![CDATA[General Introduction:
Hey hey, WhatsUp!!!

Let me give you some context here.
Imagine, there you are, in front of your computer, trying to solve a problem that has been disturbing you for quite a while now, you are fed up and now want to change the s...]]></description><link>https://blog.mawitgad.com/a-general-overview-of-the-dns</link><guid isPermaLink="true">https://blog.mawitgad.com/a-general-overview-of-the-dns</guid><category><![CDATA[network]]></category><category><![CDATA[networking]]></category><category><![CDATA[dns resolver]]></category><category><![CDATA[dns]]></category><category><![CDATA[web]]></category><dc:creator><![CDATA[Mawit Gad]]></dc:creator><pubDate>Sun, 12 Mar 2023 01:29:40 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1678558811881/a6e98621-26f2-48cc-9f9b-1576bfeab3f6.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-general-introduction"><strong>General Introduction:</strong></h2>
<p>Hey hey, WhatsUp!!!</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1678582376387/ef48bae1-ee13-401b-9ff0-fe0a10714e2a.webp" alt class="image--center mx-auto" /></p>
<p>Let me give you some context here.</p>
<p>Imagine, there you are, in front of your computer, trying to solve a problem that has been disturbing you for quite a while now, you are fed up and now want to change the strategy, instead of going on with the brainstorming, you resolve to research on google. you get to your web browser and type on the search “<a target="_blank" href="http://google.com">google.com</a>”, then a few clicks and scrolls later, you can finally copy and paste the solution to your problem. now if you didn't know, you probably have just made use of the DNS right there.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1678582434137/6b52d9e5-7a8f-41c6-ad70-f4ca2f8d48cc.webp" alt class="image--center mx-auto" /></p>
<h2 id="heading-introduction-to-dns">Introduction to DNS</h2>
<h3 id="heading-what-is-dns"><strong>What is DNS?</strong></h3>
<p>DNS stands for (domain name services) which when translated to English just means a service that makes websites have user-friendly addresses.</p>
<p>You probably already know that the internet is the interconnection of millions of computers and servers around the world (if you didn't then now you do), and for data to circulate in this mushy mess of a network in all reliability, all of these devices must have unique identifiers or addresses, at the computer level these addresses are known as Ip addresses and are just a series of digits of the form 204.0.45.88.</p>
<p>Since the internet is meant to be used by humans, it is not obvious that people will willingly memorize a unique series of digits for each and every web service that they wish to access (given that I had a hard time memorizing my own phone number), there was therefore a need to make these addresses more human appealing thus the birth of Domain names, which in other words are human understandable names that are being mapped to IP addresses of web servers by, you guessed it, <strong>The DNS</strong>.</p>
<h3 id="heading-how-does-dns-work"><strong>How does DNS work?</strong></h3>
<p>it all starts when you click on the enter button after typing a domain name like “<a target="_blank" href="http://google.com">google.com</a>” in your browser’s search bar. These are the processes that occur thereafter.</p>
<ul>
<li><p>the browser checks if it has the corresponding IP address for that domain name in the cache. if it does, it will directly make the query to the server at this address (in this case the DNS will not be involved), if not it will make a query to a local DNS resolver.</p>
</li>
<li><p>there are two types of DNS resolvers, iterative and recursive resolvers. your query will most likely be made to a recursive resolver so we will go with that for now.</p>
<p>  The recursive resolver will make a query to a root nameserver around the globe, this server contains the information about top-level domain nameservers(TLDs) which are servers that holds information about domain names with the same top-level domains for example .com, .dev, .org, etc.</p>
</li>
<li><p>the root nameserver will make a query to the top-level domain nameservers that correspond to the domain name. this server will give information about the authoritative nameserver that has the information about this domain name, the authoritative nameservers are servers that contain detailed information about domain names including their IP addresses.</p>
</li>
<li><p>Finally, the authoritative nameserver returns the required IP address to the DNS resolver and the latter is sent back to the client which then finally makes the query to the server corresponding to this IP address.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1678584229701/38681171-196f-4898-a5b0-2966ae13c74a.jpeg" alt class="image--center mx-auto" /></p>
<h2 id="heading-types-of-dns-resolvers">Types of DNS resolvers:</h2>
<p>There are two types of DNS resolvers:</p>
<h3 id="heading-recursive-resolvers"><strong>recursive resolvers:</strong></h3>
<p>These resolvers follow the same process as detailed above, they initiate a query which is then called again multiple times by the different nameservers at the different levels of the nameservers hierarchy until the IP address is retrieved at the end.</p>
<p>This is a more effective pattern compared to iterative resolving because the path to the IP address is been determined effectively at every stage of the process and is not necessarily regulated or controlled by a central node or entity.</p>
<h3 id="heading-iterative-resolvers"><strong>iterative resolvers:</strong></h3>
<p>Here, the resolvers follow a different pattern, it is like searching for a friend at a massive party, you ask John where Mary is and he tells you to ask Jake, then you ask Jake and he tells you to ask Lola and then you do before Lola tells you that Mary is right behind you(well… that's the best I could pull off as analogy for now).</p>
<p>In the same way, the resolver will query the root nameserver, then the latter will return information about the corresponding TLD server, then the resolver will now query the TLD server which will return the details about the corresponding authoritative nameserver(remember when I say corresponding here I mean in relation to the domain name for whom we are finding the IP address).</p>
<p>Finally, the resolver will query the corresponding authoritative name server which will return the desired IP address.</p>
<p>And then from there, the browser can now use this IP address to fetch the data or site it needs from the corresponding web server.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1678584206126/8d42b67a-c5a3-4741-ab0a-74b47d7318d2.jpeg" alt class="image--center mx-auto" /></p>
<h2 id="heading-conclusion"><strong>conclusion:</strong></h2>
<p>So now, I hope you have a better understanding of what DNS is and means, and that you will apply this knowledge to save the world(I mean… it's possible right?)</p>
<p>Yeah… there you go, leave your thoughts in the comments so I can learn from you too or answer your questions, and feel free to engage with me through my LinkedIn in my profile bio. till next time, happy coding.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1678584167892/1bd1ddca-ce00-4845-8abf-c20825a56bfa.gif" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[An overview of React Query.]]></title><description><![CDATA[Hey there.
So yea, you probably have or are about to start a react project, you know you are going to be interacting with a server, and so you are looking for the best way to do so given your needs. you probably have heard of React Query and you woul...]]></description><link>https://blog.mawitgad.com/an-overview-of-react-query</link><guid isPermaLink="true">https://blog.mawitgad.com/an-overview-of-react-query</guid><category><![CDATA[React]]></category><category><![CDATA[react-query]]></category><category><![CDATA[reactquery]]></category><dc:creator><![CDATA[Mawit Gad]]></dc:creator><pubDate>Fri, 09 Dec 2022 20:07:51 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1670616127635/6AlC1UDPN.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey there.</p>
<p>So yea, you probably have or are about to start a react project, you know you are going to be interacting with a server, and so you are looking for the best way to do so given your needs. you probably have heard of React Query and you would love to get an overview of what it is and if it is worth it for your project. well, here I got you covered. This article is aimed at helping you understand the scope and purpose of React Query.</p>
<h2 id="heading-what-is-react-query">what is React Query</h2>
<p>React Query is a library you can use to synchronize your react-app with an external factor(in most cases a web server). this library handles a lot of stuff out of the box that would be otherwise time-consuming and possibly tedious for some, things like caching data to optimize for performance, background fetching together with cache revalidation and invalidation to always have the up-to-date data on the screen, server state update by mutation with a developer-friendly API, including APIs for advanced implementations like pagination, and infinite scroll.</p>
<p>Having seen that the React Query library probably has all you need for your data fetching, let's get into some details.</p>
<h2 id="heading-setup">setup</h2>
<p>to get access to the React Query client, you need first of all to have your react app installed and all set (I will assume here that you already know how to set up a react app, however you can leave a comment if you wish that I write an article on that.), then we will install the React Query library with the following command;</p>
<pre><code class="lang-bash">$ npm i react-query
</code></pre>
<p>or with yarn</p>
<pre><code class="lang-bash"> $ yarn add react-query
</code></pre>
<p>now that you have installed it successfully, let's set it up in your project.</p>
<p>we will need to wrap the app or at least the portion of it where we would like to use the library in a QueryClient context provider passing in it a client property. this client is an instantiated QueryClient. we do it as below.</p>
<pre><code class="lang-javascript"> <span class="hljs-keyword">import</span> { QueryClient, QueryClientProvider } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-query'</span>

 <span class="hljs-keyword">const</span> queryClient = <span class="hljs-keyword">new</span> QueryClient()

 <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
   <span class="hljs-keyword">return</span> (
     <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">QueryClientProvider</span> <span class="hljs-attr">client</span>=<span class="hljs-string">{queryClient}</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">YourApp</span> /&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">QueryClientProvider</span>&gt;</span></span>
   )
 }
</code></pre>
<p>so now we are all set.</p>
<h2 id="heading-how-to-use">How to use.</h2>
<p>we can therefore start using the library to fetch data from our server in any child component as below</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { useQuery} <span class="hljs-keyword">from</span> <span class="hljs-string">'react-query'</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">YourApp</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">//call the useQuery hook as below to make a fetch</span>
   <span class="hljs-keyword">const</span> { isLoading, error, data } = useQuery(
    <span class="hljs-comment">//pass in a key to ease the caching process</span>
    <span class="hljs-string">'repoData'</span>,

    <span class="hljs-comment">//then pass in a callback function that will return a promise</span>
    <span class="hljs-comment">//that resoves with the data or throws an error </span>
    <span class="hljs-function">() =&gt;</span>
     fetch(<span class="hljs-string">'https://api.github.com/repos/tannerlinsley/react-                  query'</span>).then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span>
       res.json()
     )
   )

    <span class="hljs-comment">//use the isLoading flag to display the loading ui</span>
   <span class="hljs-keyword">if</span> (isLoading) <span class="hljs-keyword">return</span> <span class="hljs-string">'Loading...'</span>

    <span class="hljs-comment">//use the error ui to display the error ui</span>
   <span class="hljs-keyword">if</span> (error) <span class="hljs-keyword">return</span> <span class="hljs-string">'An error has occurred: '</span> + error.message

    <span class="hljs-comment">//in most cases you can safely display your data if</span>
    <span class="hljs-comment">//both above flags get passed</span>
   <span class="hljs-keyword">return</span> (
     <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
       {data}
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
   )
 }
</code></pre>
<p>Yes! it's as straightforward as that.</p>
<p>so basically to make a simple request, you get to call the useQuery hook, passing in at least two parameters;</p>
<ul>
<li><p>the key</p>
</li>
<li><p>and a callback</p>
</li>
</ul>
<p>The key will serve as an identifier for the query, which is used by the caching system. the key can be either a string. or an Array of a string and any number of serializable objects(the latter can be used for nested resources and queries with additional parameters).</p>
<p>the second parameter is the callback function, which returns a promise that resolves in the data or throws an error.</p>
<p>The user query hook can also take a third parameter, which is the options object, which can be used to further determine the behavior of the data fetching.</p>
<h2 id="heading-conclusion">conclusion</h2>
<p>Hopefully, this was enough of a guide to give you a basic understanding of the React Query library and help you see how helpful it might be to you and possibly your team.</p>
<p>here is the link to their website if you want to learn <a target="_blank" href="https://react-query-v3.tanstack.com/">more</a></p>
<p>if you have gone this far into the article, I want to say a big thank you and until next time.</p>
<p>happy coding...</p>
]]></content:encoded></item><item><title><![CDATA[You probably haven't understood react useEffect yet.]]></title><description><![CDATA[Hey there! this article is a follow-up to one of my articles where I describe why the useEffect hook renders twice and how to stop it possibly.
The useEffect hook is a very used and thus popular hook in the react's ecosystem, it serves its purpose wh...]]></description><link>https://blog.mawitgad.com/you-probably-havent-understood-react-useeffect-yet</link><guid isPermaLink="true">https://blog.mawitgad.com/you-probably-havent-understood-react-useeffect-yet</guid><category><![CDATA[useEffect]]></category><category><![CDATA[React]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Mawit Gad]]></dc:creator><pubDate>Wed, 30 Nov 2022 12:35:58 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1669811276301/71tA2rf63.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey there! this article is a follow-up to one of my articles where I describe why the useEffect hook renders twice and how to stop it possibly.</p>
<p>The useEffect hook is a very used and thus popular hook in the react's ecosystem, it serves its purpose when used correctly but can represent a 'foot gun' when used incorrectly and so resulting in a lot of hate from some devs. Actually, the useEffect hook is less of a dangerous hook or tool, and more of a misunderstood one. In order to fully enjoy the potential of this hook, one must understand the initial purpose for which the hook was designed and published.</p>
<h3 id="heading-what-is-useeffect-for">What is useEffect for?</h3>
<ol>
<li>The useEffect hook can be seen as an "escape hatch" as described by the <a target="_blank" href="https://beta.reactjs.org/apis/react/useEffect#useeffect">React Docs Beta</a>, this means that the useEffect serves as a portal through which you can synchronize your app's states with external systems, these could be a subscription to a server's services, a non-react widget, or other external services.</li>
</ol>
<p>let's look at an example here. let's say we have an app that makes a connection with a server for a particular component, the connection might need some arguments like some id from the react component, and should always be in sync with these arguments. in order to create this connection and keep it in sync with our app's states, the useEffect hook would be suitable</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> server = <span class="hljs-keyword">new</span> Server()

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">app</span>(<span class="hljs-params">{id}</span>)</span>{

  useEffect(<span class="hljs-function">()=&gt;</span>{
    server.connect(id)
    <span class="hljs-keyword">return</span> <span class="hljs-function">()=&gt;</span> server.disconnect()
  },[id])

}
</code></pre>
<p>notice how I added the id prop to the dependency array so that the connection will always be remade when the id changes and thus always be In sync with the latest Id. Also, notice how I added a clean-up function to maintain consistency and avoid bugs caused by multiple unnecessary connection instances.</p>
<ol>
<li><p>You could also use useEffect to fetch data from the server for use in your application, but it is mostly advised to use a framework for that task because while it is possible, the odds of you falling into issues like "race conditions", "untrue dependency array", and other issues are high. some frameworks that might serve the purpose here are,</p>
<p>-react query</p>
<p>-remix</p>
<p>-useSWR()</p>
<p>-and the use() function which is relatively new and cool.</p>
</li>
</ol>
<h3 id="heading-what-not-to-do-with-useeffect">What not to do with useEffect</h3>
<p>Hopefully, you now understand that the useEffect hook is more for synchronizing than performing effects in our apps. therefore if you are having useEffects all over the place in your code then you are probably using it wrong. Here are some instances where you might be using the useEffect wrongly.</p>
<h4 id="heading-setting-derived-state-one-of-the-most-common-mistakes-made-with-the-useeffect-hook-is-to-use-it-for-listening-to-changes-on-some-props-in-order-to-update-some-other-variables-accordingly-this-is-an-inefficient-way-of-using-the-hook-a-better-alternative-would-be-to-eliminate-the-useeffect-hook-altogether-and-just-set-the-variable-directly-as-a-standard-pure-variable-in-the-components-scope-the-variable-will-always-be-recalculated-whenever-any-of-its-constituent-state-variables-changes-for-example-below-is-a-bad-followed-by-a-good-practice-when-setting-derived-states">Setting derived state: One of the most common mistakes made with the useEffect hook is to use it for listening to changes on some props in order to update some other variables accordingly. this is an inefficient way of using the hook. a better alternative would be to eliminate the useEffect hook altogether and just set the variable directly as a standard pure variable in the component's scope. the variable will always be recalculated whenever any of its constituent state variables changes. for example below is a bad followed by a good practice when setting derived states.</h4>
<pre><code class="lang-javascript"><span class="hljs-comment">// bad practice</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">app</span>(<span class="hljs-params"></span>)</span>{

  <span class="hljs-keyword">const</span> [ state , setState ] = useState()
  <span class="hljs-keyword">const</span> [ derived , setDerived ] = useState()

  useEffect(<span class="hljs-function">()=&gt;</span>{
    setDerived(state + <span class="hljs-number">1</span>)
  },[state])

  <span class="hljs-comment">// app statements continue...</span>
}

<span class="hljs-comment">// better</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">app</span>(<span class="hljs-params"></span>)</span>{

  <span class="hljs-keyword">const</span> [ state , setState ] = useState()
  <span class="hljs-keyword">const</span> derived = state + <span class="hljs-number">1</span>

  <span class="hljs-comment">// app statements continue...</span>
}
</code></pre>
<h4 id="heading-setting-any-state-it-might-be-tempting-at-times-to-do-some-activities-in-the-useeffect-that-mutates-the-state-of-the-app-this-is-actually-a-very-dangerous-practice-and-can-easily-lead-to-an-infinite-loop-of-rerenders-this-also-is-one-of-the-reasons-why-it-is-advised-to-use-a-framework-for-data-fetching-fetching-data-yourself-with-useeffect-will-get-data-that-will-at-some-point-be-stored-in-state-and-if-not-well-managed-can-easily-cause-rerenders-in-a-loop">Setting any state: it might be tempting at times to do some activities in the useEffect that mutates the state of the app. this is actually a very dangerous practice and can easily lead to an infinite loop of rerenders. this also is one of the reasons why it is advised to use a framework for data fetching. fetching data yourself with useEffect will get data that will at some point be stored in state and if not well managed can easily cause rerenders in a loop.</h4>
<h4 id="heading-sunning-event-handles-even-though-it-is-called-the-useeffect-hook-it-is-not-really-suitable-for-effects-caused-by-events-the-best-way-to-handle-your-events-is-in-the-event-handlers-close-to-the-state-changes-rather-than-listening-to-changes-in-useeffect-to-run-effects">Sunning event handles: even though it is called the useEffect hook, it is not really suitable for effects caused by events, the best way to handle your events is in the event handlers close to the state changes, rather than listening to changes in useEffect to run effects.</h4>
<h4 id="heading-component-initialized-variables-you-should-be-careful-about-using-objects-declared-in-the-same-component-as-the-useeffect-these-objects-should-not-be-put-in-the-dependencies-array-as-this-will-cause-the-useeffect-to-run-every-time-the-component-is-rendered-due-to-the-fact-that-for-each-render-a-new-and-different-instance-of-the-object-is-created">component initialized Variables: You should be careful about using objects declared in the same component as the useEffect, these Objects should not be put in the dependencies array as this will cause the useEffect to run every time the component is rendered due to the fact that for each render, a new and different instance of the object is created.</h4>
<h3 id="heading-conclusion">Conclusion</h3>
<p>In conclusion to this article, the useEffect hook is a very useful tool that just needs to be understood and used for its purpose. when used correctly, it offers so much value.</p>
<p>so guys those are some insights on the useEffect hook, to continue learning I would advise you to go to the new react docs <a target="_blank" href="https://beta.reactjs.org/apis/react/useEffect#useeffect">React Docs Beta</a>, and till next time, happy coding.</p>
<p><img src="https://i.chzbgr.com/full/8270686464/h7779056E/coding-is-an-art" alt="happycoding" /></p>
]]></content:encoded></item><item><title><![CDATA[Redux, Redux Toolkit, or React-redux? which should I learn?]]></title><description><![CDATA[Hey there Beautiful person!

Some context
Recently I started working on a web project and decided to use Redux for state management, as it is going to be a rather big project and I wanted to learn Redux along the way.
So I went to the Redux website t...]]></description><link>https://blog.mawitgad.com/redux-redux-toolkit-or-react-redux-which-should-i-learn</link><guid isPermaLink="true">https://blog.mawitgad.com/redux-redux-toolkit-or-react-redux-which-should-i-learn</guid><category><![CDATA[next redux wrapper]]></category><category><![CDATA[Redux]]></category><category><![CDATA[React]]></category><category><![CDATA[react redux]]></category><category><![CDATA[redux-toolkit]]></category><dc:creator><![CDATA[Mawit Gad]]></dc:creator><pubDate>Thu, 17 Nov 2022 20:13:48 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1668716183094/wAkV9R6n-.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-hey-there-beautiful-person">Hey there Beautiful person!</h2>
<p><img src="https://media.giphy.com/media/143qWPF33HtSTK/giphy.gif" alt="hey the gif" /></p>
<h3 id="heading-some-context">Some context</h3>
<p>Recently I started working on a web project and decided to use Redux for state management, as it is going to be a rather big project and I wanted to learn Redux along the way.</p>
<p>So I went to the Redux website to get started and oh boy was I blown with a tsunami of overwhelming information. 
In the beginning, I just knew I wanted to use redux for my state management and thought "yea easy... let me just hop on there and start learning redux"
little did I know I'd first need to know what I needed to learn in the redux ecosystem before even starting to learn any of the multiple products (3 to be exact) involved in this ecosystem.</p>
<p><img src="https://media2.giphy.com/media/SUK7gnxFOxpqJGwd3v/giphy.gif?cid=ecf05e47xlpwupe2p14uvkq47sbhhlzhsn34l5yrmj8xse2b&amp;rid=giphy.gif" alt="Lucky you gif" /></p>
<p>well luckily for you, I've gone through the hassle of understanding these different packages so you won't have to start from scratch or be overwhelmed as I was.</p>
<p>now going straight to the subject of the matter,</p>
<h3 id="heading-redux">Redux</h3>
<p>Redux is a state management framework for javascript applications that manages and stores the application's in a predictable way. 
Now Redux is the base of everything. I would advise getting at least the bases and understanding its concepts and terminologies. for that here Is a youtube playlist that tremendously helped me <a target="_blank" href="https://www.youtube.com/watch?v=9boMnm5X9ak&amp;list=PLC3y8-rFHvwheJHvseC3I0HuYI2f46oAK">codevolution</a>.
While it might have been published in 2019, I personally went through it, and I love the way the course is Structured and aimed at getting you to understand intuitively the redux pattern.</p>
<h3 id="heading-redux-toolkit">Redux Toolkit</h3>
<p>Redux Toolkit is a wrapper over Redux, it is the recommended way of writing redux code since it has a lot of advantages over writing code in plane Redux (I guess..??).
Redux Toolkit is just built upon Redux. It reduces the amount of boilerplate code you need to write to get started with a Redux project and so it makes it easier to use Redux.
So since it is just a wrapper, it is not obligatory to use, you might still do well without it entirely, but if you want to make your life easier I'd recommend learning it too. Here is a youtube playlist that helped me with understanding Redux Toolkit <a target="_blank" href="https://www.youtube.com/watch?v=0awA5Uw6SJE&amp;list=PLC3y8-rFHvwiaOAuTtVXittwybYIorRB3">codevolution</a>.</p>
<p>React-Redux is a package written by the Redux team to facilitate the integration of Redux with React js components. it makes it so easy to retrieve and set state from the redux store and automatically update the UI as you would expect. This package is for my React.js guys, but as you might already know, Redux as a framework is not tightly coupled to react. the React-Redux package is just a third-party package written by the Redux team to ease integration with React js. both of the youtube playlists recommended above get into React-Redux at the end.
Note, you can use a combination of React-Redux and Redux, or React-Redux and Redux-toolkit for your react projects</p>
<h3 id="heading-bonus-next-react-wrapper">Bonus (Next-React-Wrapper)</h3>
<p>Okay, you might be going " hey Mawit, you said there are 3 packages in the redux ecosystem, where does 'Next-React-Wrapper' come from?? huh?? " </p>
<p><img src="https://media3.giphy.com/media/NJkIw5wfnM3e0/giphy.gif?cid=ecf05e479twjp4hgkvo40wye9w5dykuc9u3cnv8b3jec0xgw&amp;rid=giphy.gif" alt="Lucky you gif" /></p>
<p>well fellow, let me explain.
Actually, using Redux in Next js seems to be a little more complicated, I mean, when you intend to use the State's values on the server side functions like getServerSideProps or GetStaticProps. so someone decided to start an open-source project that would help with just that. this package is not maintained by the redux team themselves ( I might be wrong) and that's why it doesn't appear on Redux's website.
Now, personally, I haven't really used it yet, though it seems to be important too, I just want to bring it to your attention, so it's up to you what you want to do with the awareness.
You should also understand that Next-React-wrapper is not mandatory for every Next.js app. as long as all your state management is run on the client side I think you are still fine without it.</p>
<p>So there you go guys, I hope this was helpful to you, I hope this was worth your time, and till next time, Happy coding.</p>
<p><img src="https://media3.giphy.com/media/KfwyWfTwMu1FG0XhO8/giphy.gif?cid=790b7611677b6e055103fab83f1d4e284768336b8e2ac8e3&amp;rid=giphy.gif&amp;ct=g&quot;" alt="Lucky yougif" /></p>
]]></content:encoded></item><item><title><![CDATA[why do your react components render twice?]]></title><description><![CDATA[intro
Yes! I can imagine how frustrating it can be huh? 

You just started your new react app, or you have been working on a project for a while, and have noticed the unpleasant double rendering of your react components which affects especially the u...]]></description><link>https://blog.mawitgad.com/why-do-your-react-components-render-twice</link><guid isPermaLink="true">https://blog.mawitgad.com/why-do-your-react-components-render-twice</guid><category><![CDATA[React]]></category><category><![CDATA[ReactHooks]]></category><category><![CDATA[js]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[useEffect]]></category><dc:creator><![CDATA[Mawit Gad]]></dc:creator><pubDate>Fri, 17 Jun 2022 08:40:27 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1655453804768/4cBzyzn1U.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-intro">intro</h2>
<p>Yes! I can imagine how frustrating it can be huh? </p>
<p><img src="https://media.giphy.com/media/xT9IgzUuC5Ss6ZnTEs/giphy-downsized-large.gif" alt="i understand" />
You just started your new react app, or you have been working on a project for a while, and have noticed the unpleasant double rendering of your react components which affects especially the useEffect hook(where most devs write their effects right?), firing functions, API calls, and other tasks twice each time the app loads. </p>
<p>Now you have searched through your code for possible causes but can't find it? well, I think I have an explanation for that. so without any further ado let's get started.
<img src="https://media.giphy.com/media/xZsfgxuBCMdLU9I6Hl/giphy.gif" alt="lets start" /></p>
<h2 id="heading-react-strict-mode">React Strict Mode</h2>
<p>That's him😀! the hidden trouble maker.
The latest version of React.js  is v18.2.0, and these latest versions(from v18.0.0 to the most recent) come with some updates that seem controversial to many developers while more appropriate to others</p>
<p>One of these updates is the <a target="_blank" href="https://reactjs.org/docs/strict-mode.html">StrictMode</a>, which from the docs :</p>
<blockquote>
<p>is a tool for highlighting potential problems in an application.</p>
</blockquote>
<p>This just means that it's a tool that helps in writing better code for better applications.</p>
<p>The mechanism by which your components are rendered twice is as thus:</p>
<ul>
<li><p>On render, the component will make the first mount.</p>
</li>
<li><p>Then react will unmount to make checks on how your code handles unmounts which is very useful for cases where you have subscriptions to APIs that need to be cleaned up to avoid memory leakage and other performance issues.</p>
</li>
<li><p>And finally, the component is mounted a second time.</p>
</li>
</ul>
<h2 id="heading-should-you-try-walkarounds">Should you try walkarounds?</h2>
<p><img src="https://media.giphy.com/media/26FmQ6EOvLxp6cWyY/giphy.gif" alt="walkaround" />
Well, it's really an 'it depends' kind of a thing. I personally think the react strict mode makes for better code writing and improved software development practices, even as I won't deny the fact that I felt like it impacted my development experience in the beginning.
You might want to disable the strict mode if you are working on a more trivial project or some project where you value the development experience more and just want to get something done.</p>
<h2 id="heading-how-to-disable-strict-mode">How to disable strict mode</h2>
<p>if you decide to disable the strict mode in your project, it's quite easy. If you bootstrapped your project with the create-react-app command then in the index.js file you will most likely find a <em>React.StrictMode</em> component wrapping your <em>App</em>
component. You can just delete the component's open and close tags and voila.</p>
<p>On State</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-title">React</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">ReactDOM</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'react-dom/client'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">App</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./App'</span>;


const root <span class="hljs-operator">=</span> ReactDOM.createRoot(document.getElementById(<span class="hljs-string">'root'</span>));
root.render(
  <span class="hljs-comment">// react strict mode is on by default, so we need to turn it off</span>
  <span class="hljs-operator">&lt;</span>React.StrictMode&gt;
    <span class="hljs-operator">&lt;</span>App <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
  <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>React.StrictMode&gt;
);
</code></pre><p>Off State</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-title">React</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">ReactDOM</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'react-dom/client'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">App</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./App'</span>;


const root <span class="hljs-operator">=</span> ReactDOM.createRoot(document.getElementById(<span class="hljs-string">'root'</span>));
root.render(
  <span class="hljs-comment">// now react strict mode is off</span>
    <span class="hljs-operator">&lt;</span>App <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
);
</code></pre><h2 id="heading-better-alternative">Better Alternative</h2>
<p>Now that you know how to stop the until now unexpected rerendering of your components in react, let me talk you into why you would be better off leaving it on.</p>
<p>Actually, as devs and builders of the future, it is our responsibility to write sustainable, efficient, and maintainable code in order to develop more robust software. That should be our priority and that is where react's <a target="_blank" href="https://reactjs.org/docs/strict-mode.html">StrictMode</a> comes in place, to help us write better and more performant code.</p>
<h2 id="heading-conclusion">conclusion</h2>
<p><img src="https://media.giphy.com/media/l0HlBgVQygUhjPSb6/giphy.gif" alt="conclusion" />
now I hope you know why you had those double logs in your console. Or those double API calls that you didn't expect, but now there remains a question: </p>
<blockquote>
<p>"how do I stop my expensive functions from running twice without disabling strict mode?" </p>
</blockquote>
<p>I think I will make that the topic for my next article so please leave a comment if you are interested or awaiting.
and now, let me wish you a great day, and to the next article, happy coding! </p>
<p><img src="https://media.giphy.com/media/KfwyWfTwMu1FG0XhO8/giphy.gif" alt="happy coding" /></p>
]]></content:encoded></item><item><title><![CDATA[welcome to my blog]]></title><description><![CDATA[my stories are pending and soon to come.. 
please keep patiance...]]></description><link>https://blog.mawitgad.com/welcome-to-my-blog</link><guid isPermaLink="true">https://blog.mawitgad.com/welcome-to-my-blog</guid><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Mawit Gad]]></dc:creator><pubDate>Thu, 28 Oct 2021 12:01:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1635422335684/VcqdZYsiC.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>my stories are pending and soon to come.. 
please keep patiance...</p>
]]></content:encoded></item></channel></rss>