<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Enea Gega on Medium]]></title>
        <description><![CDATA[Stories by Enea Gega on Medium]]></description>
        <link>https://medium.com/@Enea-Gega?source=rss-5b071610145e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dijitlpgqzJgztxh5yFxWQ.jpeg</url>
            <title>Stories by Enea Gega on Medium</title>
            <link>https://medium.com/@Enea-Gega?source=rss-5b071610145e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 01:26:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Enea-Gega/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Why useEffect is important?]]></title>
            <link>https://medium.com/@Enea-Gega/why-useeffect-is-important-1dc00f72697f?source=rss-5b071610145e------2</link>
            <guid isPermaLink="false">https://medium.com/p/1dc00f72697f</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[useeffect]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Enea Gega]]></dc:creator>
            <pubDate>Wed, 01 Feb 2023 23:41:51 GMT</pubDate>
            <atom:updated>2023-02-01T23:41:51.207Z</atom:updated>
            <content:encoded><![CDATA[<p>Shortly said, useEffect allows you to run a piece of code after rendering, so you can handle updates, side-effects, and cleanup.</p><p>By using a simple example I will try to explain the importance of useEffect and the advantages it provides.<br>Here is the structure of the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*lg-WjF5mKxrBBtFCfeVEiQ.png" /></figure><p>Here is the login page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xv7z4E9aFhpSD6kR0-rZhw.png" /></figure><p>Here is the main page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ouVVUzx1o5G6ujCZmufA8A.png" /></figure><p>The logic is simple, we have two main pages, the Login page and the Homepage. When we press the login button the Home component is shown and when we press the logout button the login component is shown back.<br>No real backend authentication is happening and we just need an email format and a password longer than 8 characters is enough to “log in”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/869/1*RPWg1U7OA_pySQe1Gqzdyg.png" /><figcaption>Enea Gega</figcaption></figure><p>We also have used the useState hook here and gave a value of false to our isLoggedIn constant. As you know we use the setIsLoggedIn setter function to modify the state. We use the login and logout handlers to change the state to false or true accordingly and we use conditional rendering to determine which component should be displayed.<br>Everything seems fine till here as long as the frontend is concerned but what about when we have to get the data from a backend server? Also, it works just till we reload since nothing is stored. To get rid of that problem we will use the localStorage here. So whenever we click logout we setItem in local storage as “0” and whenever we press login we setItem to “1” as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*JnN1Pjf7DWPIhCiGAQGx8A.png" /><figcaption>login logout handlers</figcaption></figure><p>You can say, well we can get the items from the localStorage by using localStorage.getItem() method and put them into a condition like shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/912/1*SaFbTKFoazlz_eahgH_5KQ.png" /><figcaption>conditional rendering</figcaption></figure><p>After doing so we immediately get a blank page and when we open the console that is what we see. Something that we should avoid like the “Plague”, an infinite loop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*ueKAq0sPOV53bRpiBB6qRg.png" /><figcaption>useEffect infinite loop</figcaption></figure><p>So why is this happening?<br>Because we are updating the isLoggedIn state in an if-else statement, which means that every time the render method is called, the state will change and trigger a re-render, leading to an infinite loop.<br>So what is the solution, well you have guessed right, the useEffect Hook.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*GHgTBD8wlt578tlF_9o-ug.png" /></figure><p>We should take the if statement inside the useEffect hook.<br>This solution will resolve the infinite loop issue in our code by updating the isLogged state outside of the render method, using the useEffect hook.</p><p>The useEffect hook is run only when storedUsername dependency changes, so the isLoggedIn state will be updated only when the stored username changes. This is how we can prevent the infinite loop.</p><p>Note: The structure belongs to Maximilian Schwarzmüller, and his React Bootcamp, which I occasionally open when I need a React refresh.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1dc00f72697f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Update Objects in React State]]></title>
            <link>https://medium.com/code-tricks/how-to-update-objects-in-react-state-f971665ddcb5?source=rss-5b071610145e------2</link>
            <guid isPermaLink="false">https://medium.com/p/f971665ddcb5</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[state]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Enea Gega]]></dc:creator>
            <pubDate>Wed, 26 Oct 2022 01:11:19 GMT</pubDate>
            <atom:updated>2022-10-26T01:11:19.342Z</atom:updated>
            <content:encoded><![CDATA[<p>Updating Objects in React State may be a bit complicated, or it was for me, or at least till now. In this short story, I will try to write what you should do and what not, to avoid common mistakes. I have created a simple react app where I will use display some basic information about myself by using State Objects and onMouseEnter + onMouseLeave.</p><p>Firstly let&#39;s import UseState and define our State and its setter function. (profile and SetProfile)<br>Since our aim is to learn how to update the state in case it is an object our object will contain three keys <em>firstname, surname, age</em>, and their values which will be booleans(initially set as false). We will need to trigger an event to change their value, in this case, we will use onMouseEnter and onMouseLeave.<br> Currently, our app will just display the h3 elements and we need to create a conditioned logic by updating the State in order to display the h2 elements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wmG0VVksIu5qE52yf5jaIg.png" /></figure><p>Now it is time to trigger the onMouseEnter and onMouseLeave events and change the state. It sounds easy right? For example when hovering (mouseEnter)“&lt;h3&gt;Display my surname&lt;/h3&gt;” we can just update profile by saying SetProfile(“profile.name = true ”) and then SetProfile(“profile.name = false”) again while hovering out. Well not exactly, if we console.log the profile we will see that we have an object when the component is first rendered but then profile turns to a boolean value and we can not reach the object anymore.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hRRNCn1CSVzDe42HNyvXRw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*KgnKG28v2UgApKQ0axuFJQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qo5oZD1gQWnz9OkSEhiKxQ.png" /></figure><p>As you can see we have lost the object and now we can not reuse it anymore. The right way to do it is to copy the entire object by using the spread operator and then updating the first name. So this would be the right approach <strong>setProfile({…profile, firstname: true}) </strong>and then back to false<strong> setProfile({…profile, firstname: false).<br></strong>As you can see everything works fine now and our state gets updated every time we hover in and out</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NFA0D86w57aETe_B0CDYdg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TH73kAYwjSy2isMAfO7YaQ.png" /></figure><p>The rest is pretty easy. We can implement the same logic to the rest of the elements so we can update the surname and age. After doing it we need to implement our logic to our h2 elements and display them whenever we hover over their respective h3 elements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v5gvq7hqeII7NcUswNCEeQ.png" /></figure><p>As you may have noticed I have used the <strong>Logical And</strong> to display the h2 elements whenever I update the state accordingly. Here is the MDN definition of Logical And <em>(The logical AND (</em><em>&amp;&amp;) operator (logical conjunction) for a set of boolean operands will be </em><em>true if and only if all the operands are </em><em>true. Otherwise it will be </em><em>false.)</em></p><p>If you find it confusing you can just use ternary as in the examples below.<br>You can check the results by looking at the gif below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*1ynKRmWB5UW9SKssnnSvHQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*SBxNHpW7wOr7vf-9eiuX5A.gif" /></figure><p>Notes: Here is a <a href="https://github.com/EGega/randomUser">GitHub </a>Repo where I have implemented the same logic and here you can add my <a href="https://www.linkedin.com/in/eneagega/">Linkedin</a> profile in case you are interested to connect.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f971665ddcb5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/code-tricks/how-to-update-objects-in-react-state-f971665ddcb5">How to Update Objects in React State</a> was originally published in <a href="https://medium.com/code-tricks">Code Tricks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to make a weather app as a complete beginner by using the Open Weather API ?]]></title>
            <link>https://medium.com/code-tricks/how-to-make-a-weather-app-as-a-complete-beginner-by-using-the-open-weather-api-2f6f8cb04d66?source=rss-5b071610145e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f6f8cb04d66</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[weather]]></category>
            <dc:creator><![CDATA[Enea Gega]]></dc:creator>
            <pubDate>Wed, 28 Sep 2022 15:53:07 GMT</pubDate>
            <atom:updated>2022-09-28T16:02:15.116Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to make a weather app as a complete beginner by using the Open Weather API ?</h3><p>Hello, dear “developer candidate” out there.<br>I am a beginner programmer and a beginner writer so I am probably a better person to comfort you rather than a senior developer, as we are in similar positions.<br>I would like to share my first weather app and my solution with you.</p><p>Firstly I would like to state that I challenged myself to complete the application without looking at online resources (Do not get me wrong, 80% of programming is probably checking stuff online. However, I believe that it is a good idea to challenge yourself sometimes)</p><p>To begin with, this is the app and how it looks. Now let’s dive into details and learn how to create a similar app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jyFH1vb1le-Hn8DxowWCZw.png" /></figure><p>As we usually do we start by selecting the elements that we are going to work with. <br>We need input to reach the value entered, submit to add a click event, and the warning to display the alert in case of empty or invalid values.</p><figure><img alt="Query Selectors" src="https://cdn-images-1.medium.com/max/700/1*KMWJSU6xwSUXeTJs9Qw0uQ.png" /></figure><p>I also created an empty array where I am going to push the names of the cities that the API returns to us. We will need these values to check if a city has been displayed before or not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*bNeTENKtPHAlA2DSJsFtzw.png" /></figure><p>We are going to give a clicking event to the submit button so we can use the .addEventListener method. The first thing that we need to handle is the empty queries and display a “warning” message to the user. We can check it by checking if the input.value === “” or not. However, there is a more elegant way of doing it by just saying if<strong>(!input.value)</strong> which can be translated as if the value of the input is a true value. We can just write a warning message as innerText. Since I didn’t want the warning to be displayed for a long time I created a setTimeout function to adjust the innerText back to an empty value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*npcN4smc8-5BBhCdOpPqkg.png" /></figure><p>Everything else will happen inside the else block.<br>Firstly I create a function named <strong>getTheCity()</strong> where I get the data from the API. I have used the fetch API but you can just use Axios or Async Await.<br>Since I want to enter API to get the city that the user inputs on the screen I can just use <strong>input.value</strong> within the template literals. Please make sure to use the <strong>toLowerCase()</strong> method so it won’t give you a headache when you check for duplicate values inside the array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VRla2mxeqYqRJU2DUFbcxQ.png" /></figure><p>For the sake of simplicity, I didn’t deal too much with the error handling and wrote just a simple line of code where I check if the response (res) returns as ok or not. Usually, the API will return a response as not ok (a response not starting with a 200 code) in case of invalid entries so I decided to display a simple “please enter a valid city” warning. Of course, you can change it and make the error handling more efficient. As you already know I used a simple setTimeout to clear the innerText of the warning element. <br>Outside the if block, make sure to “transform” the response to a javascript object by using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Response/json"><strong>.json()</strong></a> method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zPd89P_Rd9J1ddieoL7enA.png" /></figure><p>Now that we have valid data we can use<strong> .then()</strong> to deal with the data and display whatever we want to display. However, before handling all the conditions in the data we should create a function to display the cities.<br>We will come back to this part and explain it in detail after creating the function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EaSa3reEh4-ZR8Nt5OtKHA.png" /></figure><p>I have decided to call the function<strong> displayTheCity</strong> and enter a “data” parameter. Firstly we need to select the empty <strong>weatherContainer</strong> that we have created inside the HTML. (This is my repo <a href="https://github.com/EGega/WeatherApp">link</a> so you can just copy-paste the HTML and CSS from there).<br>It is always a great idea to destructure the object instead of using dots or square brackets to reach the value.<br>Here is a very useful MDN <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">link</a> to learn more about destructuring.</p><p>I am European and we use Celsius to measure the temperature so that is the reason why I have transformed the default Kelvin value to Celsius. If you are an American you can of course use Fahrenheit. Here is a Reddit <a href="https://www.reddit.com/r/MapPorn/comments/2v635l/countries_that_use_fahrenheit_versus_those_who/">link</a> to the countries that use Fahrenheit. See, it is only you.</p><p>Since we just need the name of the city to check if a city is a duplicate value it is enough to push the city name to the array. I advise you to use the <strong>toLowerCase()</strong> method thus will be easier to check for duplicate values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s69lUH3nj3LzUPMRw3Zvnw.png" /></figure><p>I have created a card before and just copied it to the innerHTML of the <strong>weatherContainer</strong>. Please note how I have used template literals and the destructured variables to generate new values each time a request is sent to the server. I used the <em>name, degree, country description, and icons</em> as elements that I wanted to display. You can get the icons from my repo that I will attach at the end of the article.<br>I wanted that each time I search for a city the displayed card is the first one in line so that’s why I have used (+ <strong>weatherContainer.innerHTML</strong>) at the end of the backticks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2H38vm-CRAOz7NadcKCFoA.png" /></figure><p>Now that we have our function it is time to get back to our function and explain its conditions. Firstly we destructure the <em>name</em> since we need it to check for duplicate values. We can check it by using the <strong>.includes()</strong> built-in method. In case the city already exists a short warning message will be displayed and then it will be cleared after 3 seconds by using the <strong>setTimeout()</strong> method. This is optional but I have set a maximum number of 4 cities to be displayed for styling purposes. You can totally ignore this condition or change it according to your wish. Finally, if none of the previous conditions is true we can display the city by calling the<strong> displayTheCity()</strong> function that we just created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4bO5No85sfItC7p2lH13FQ.png" /></figure><p>Immediately under the <strong>displayTheCity()</strong> function that we have created, we can call the <strong>getTheCity()</strong> function and then reset the value of our input to “” empty. Please make sure to call the function inside the initial else block.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yp4MJGOBQxIsqOj7Gm5Bxg.png" /></figure><p>If you wish you can also give functionality to the input element by adding an <strong>eventListener.</strong> Firstly you should decide which key you are going to use when calling the function(I have decided to use the key “Enter”). A simple <strong>(event.code === “Enter”)</strong> will make the job. Since we have already given all the functionality to the submit button we can just call it with the <strong>click()</strong> method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GrrCniChK2TNS1TbHnQTEg.png" /></figure><p>Here you check the <a href="https://github.com/EGega/WeatherApp">repository</a> and get the styling, markup, and icons. Please note that you can also use the default icons but the ones I have used are more stylish. I have actually downloaded them after searching online. You can also create your icons by yourself.<br>Please reach me for any questions and I am also open to pull requests.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f6f8cb04d66" width="1" height="1" alt=""><hr><p><a href="https://medium.com/code-tricks/how-to-make-a-weather-app-as-a-complete-beginner-by-using-the-open-weather-api-2f6f8cb04d66">How to make a weather app as a complete beginner by using the Open Weather API ?</a> was originally published in <a href="https://medium.com/code-tricks">Code Tricks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>